const models = require('vn-loopback/server/server').models;

describe('campaign latest()', () => {
    it('should return the campaigns from the last year', async() => {
        const now = Date.vnNew();
        const result = await models.Campaign.latest();
        const randomIndex = Math.floor(Math.random() * result.length);
        const campaignDated = result[randomIndex].dated;

        expect(result.length).toEqual(3);
        expect(campaignDated).toBeLessThanOrEqual(now);
    });

    it('should return the campaigns from the current year', async() => {
        const now = Date.vnNew();
        const currentYear = now.getFullYear();
        const result = await models.Campaign.latest({
            where: {dated: {like: `%${currentYear}%`}}
        });

        const randomIndex = Math.floor(Math.random() * result.length);
        const campaignDated = result[randomIndex].dated;
        const campaignYear = campaignDated.getFullYear();

        expect(result.length).toEqual(3);
        expect(campaignYear).toEqual(currentYear);
    });
});