Home Reference Source

application/__tests__/services/assignment-service-test.js

jest.unmock('./../../services/base-service');
jest.unmock('./../../services/error-service');
jest.unmock('./../../services/assignment-service');
jest.unmock('./../../alt.js');

import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';
import Immutable from 'immutable';
import sinon from 'sinon';
import AltTestingUtils from 'alt-utils/lib/AltTestingUtils';
import alt from './../../alt';

import {AssignmentService, AssignmentsStore} from './../../services/assignment-service';
import fetch from './../../util/mock-fetch';

describe('Assignment service', () => {
    beforeEach(() => {
        jasmine.clock().uninstall();

        sinon.stub(window, 'fetch').returns(fetch({
            'items': [
                {
                    id: 1,
                    uuid: '123-456',
                    attachment: {}
                },
                {
                    id: 2,
                    uuid: '987-654',
                    attachment: {}
                }
            ],
            'first': 1,
            'before': 1,
            'current': 1,
            'last': 1,
            'next': 1,
            'total_pages': 1,
            'total_items': 1,
            'limit': 1
        }));
    });

    afterEach(() => {
        window.fetch.restore();
    });

    it('calls fetch data', (done) => {
        AssignmentService.fetch();
        setTimeout(() => {
            expect(window.fetch.called).toEqual(true);
            expect(window.fetch.getCalls()[0].args[0]).toEqual('/v3/assignment');
            done();
        }, 100);
    });
    
    it('updates stores', (done) => {
       AssignmentService.fetch();
       
       setTimeout(() => {
           expect(AssignmentsStore.state.assignments.size).toEqual(2);
           
           done();
       }, 100);
    });

});