Home Reference Source

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

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

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 {DraftService, DraftsStore} from './../../services/draft-service';
import fetch from './../../util/mock-fetch';

describe('DraftService', () => {

    it('fetches draft', (done) => {
        sinon.stub(window, 'fetch').returns(fetch([{
            id: 1,
            user_id: 2,
            content_id: 3,
            content: 'this is a test'
        }]));

        DraftService.fetchOne('foo').then(() => {
            expect(DraftsStore.getState().draft.get('id')).toEqual(1);
            expect(DraftsStore.getState().draft.get('user_id')).toEqual(2);

            window.fetch.restore();
            done();
        });
    });

    it('creates draft', (done) => {
        sinon.stub(window, 'fetch').returns(fetch([{
            id: 1,
            user_id: 2,
            content_id: 3,
            content: 'this is a test'
        }]));

        DraftService.create('srn:tsn:test', {'content': 'fraz'}).then(() => {
            expect(window.fetch.called).toEqual(true);
            expect(window.fetch.getCalls()[0].args[0]).toEqual('/v3/draft?content=srn%3Atsn%3Atest');
            window.fetch.restore();
            done();
        });
    });

});