Home Reference Source

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

jest.unmock('./../../services/user-service');
jest.unmock('./../../services/base-service');
jest.unmock('./../../services/error-service');
jest.unmock('./../../util/mock-fetch');

jest.unmock('./../../alt');

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 {UserService, UsersStore} from './../../services/user-service';
import fetch from './../../util/mock-fetch';

describe('UserService create', () => {

    beforeEach(() => {

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

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

    it('expects search to return data', (done) => {
        const spy = sinon.spy(UserService, 'updateUsers');
        jasmine.clock().uninstall();

        UserService.search({'type': 'user', 'keywords': 'test'}).then(() => {
            expect(spy.called).toEqual(true);

            UserService.updateUsers.restore();
            done();
        });
    });

    it('expects store to update state', (done) => {
        jasmine.clock().uninstall();
        
        UserService.reset();
        setTimeout(() => {
            expect(UsersStore.state.users.size).toEqual(0);
            UserService.search({'type': 'user', 'keywords': 'test'}).then(() => {

                expect(UsersStore.state.users.size).toEqual(2);
                done();
            });
        }, 100);
    });
});