Home Reference Source

application/__tests__/util/request-test.js

jest.unmock('./../../util/request')
jest.unmock('./../../util/strings')

import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';
import sinon from 'sinon';

import Request from './../../util/request';

describe('Request utility', () => {

    beforeEach(() => {
        sinon.stub(Request, '_getLocationHref').returns(
            'http://tsn.ceo-core.localhost/ceo/content?page=1&fraz[0]=bork'
        );
    });

    afterEach(() => {
        Request._getLocationHref.restore();
    })

    it('returns path', () => {
        const loc = Request.getPath();
        expect(loc).toEqual('/ceo/content');
    });

    it('returns query obj', () => {
        const loc = Request.getQuery(false);
        expect(loc['page']).toEqual('1');
        expect(loc['fraz[0]']).toEqual('bork');
    });

    it('returns query string', () => {
        const loc = Request.getQuery();
        expect(loc).toContain('page=1');
        expect(loc).toContain('fraz[0]=bork');
    });

    it('can rebuild query', () => {
        const newQuery = Request.setQuery({'page': 99, 'sort': 'france'});
        expect(newQuery).toContain('page=99');
        expect(newQuery).not.toContain('page=1');
        expect(newQuery).toContain('sort=france');
        expect(newQuery).toContain('fraz%5B0%5D=bork');
    });

});