/**
 * The unittests for the exported functions from `categories.js`.
 */

import * as categories from './categories';
import * as communication from './communication';


describe('categories', () => {
  describe('create', () => {
    it('should make an authorised request', done => {
      const authorisedRequestSpy = spyOn(communication, 'authorisedRequest');
      authorisedRequestSpy.and.returnValue(Promise.resolve({id: 'c4t'}));

      categories.create({name: 'poes'})
        .then(() => {
          const createRequest = authorisedRequestSpy.calls.mostRecent();
          expect(createRequest.args).toEqual(['POST', '/categories', {name: 'poes'}]);
          done();
        }, fail);
    });
  });


  describe('getById', () => {
    it('should make an authorised request', done => {
      const authorisedRequestSpy = spyOn(communication, 'authorisedRequest');
      authorisedRequestSpy.and.returnValue(Promise.resolve({id: 'c4t'}));

      categories.getById('c4t')
        .then(() => {
          const getRequest = authorisedRequestSpy.calls.mostRecent();
          expect(getRequest.args).toEqual(['GET', '/categories/c4t']);
          done();
        }, fail);
    });
  });


  describe('getAll', () => {
    it('should make an authorised request', done => {
      const authorisedRequestSpy = spyOn(communication, 'authorisedRequest');
      authorisedRequestSpy.and.returnValue(Promise.resolve([{id: 'c4t'}]));

      categories.getAll()
        .then(() => {
          const getRequest = authorisedRequestSpy.calls.mostRecent();
          expect(getRequest.args).toEqual(['GET', '/categories']);
          done();
        }, fail);
    });

    it('should allow filters if they are a URLSearchParams object', done => {
      const authorisedRequestSpy = spyOn(communication, 'authorisedRequest');
      authorisedRequestSpy.and.returnValue(Promise.resolve([{id: 'c4t'}]));

      const filters = new URLSearchParams();
      filters.set('parent', 'd4ddyc4t');

      categories.getAll(filters)
        .then(() => {
          const getRequest = authorisedRequestSpy.calls.mostRecent();
          expect(getRequest.args).toEqual(['GET', '/categories?parent=d4ddyc4t']);
          done();
        }, fail);
    });

    it('should reject when something other than URLSearchParams is given as the filters', done => {
      categories.getAll('this is not an instance of URLSearchParams')
        .then(fail, done);
    });
  });

  describe('getAllWithParentId', () => {
    it('should make an authorised request', done => {
      const authorisedRequestSpy = spyOn(communication, 'authorisedRequest');
      authorisedRequestSpy.and.returnValue(Promise.resolve([{id: 'c4t'}]));

      categories.getAllWithParentId('poes')
        .then(() => {
          const getRequest = authorisedRequestSpy.calls.mostRecent();
          expect(getRequest.args).toEqual(['GET', '/categories/poes/categories']);
          done();
        }, fail);
    });
  });
});

@@ -696,32 +696,551 @@
 %0A    %7D);%0A  %7D);%0A%0A
+  describe('update', () =%3E %7B%0A    it('should make an authorised request', done =%3E %7B%0A      const authorisedRequestSpy = spyOn(communication, 'authorisedRequest');%0A      authorisedRequestSpy.and.returnValue(Promise.resolve(%7Bid: 'c4t'%7D));%0A%0A      categories.update('c4t', %7Bname: 'poes'%7D)%0A        .then(() =%3E %7B%0A          const createRequest = authorisedRequestSpy.calls.mostRecent();%0A          expect(createRequest.args).toEqual(%5B'PUT', '/categories/c4t', %7Bname: 'poes'%7D%5D);%0A          done();%0A        %7D, fail);%0A    %7D);%0A  %7D);%0A%0A
 %0A  describe('get

add missing test for update function