Browse Source

Added action tests

Oleh T 7 years ago
parent
commit
817eafafe1
3 changed files with 485 additions and 0 deletions
  1. 161 0
      client/actions/__test__/auth.js
  2. 165 0
      client/actions/__test__/settings.js
  3. 159 0
      client/actions/__test__/url.js

+ 161 - 0
client/actions/__test__/auth.js

@@ -0,0 +1,161 @@
+import nock from 'nock';
+import sinon from 'sinon';
+import { expect } from 'chai';
+import cookie from 'js-cookie';
+import thunk from 'redux-thunk';
+import Router from 'next/router';
+import configureMockStore from 'redux-mock-store';
+
+import { signupUser, loginUser, logoutUser, renewAuthUser } from '../auth';
+import {
+  SIGNUP_LOADING,
+  SENT_VERIFICATION,
+  LOGIN_LOADING,
+  AUTH_RENEW,
+  AUTH_USER,
+  SET_DOMAIN,
+  SHOW_PAGE_LOADING,
+  UNAUTH_USER
+} from '../actionTypes';
+
+const middlewares = [thunk];
+const mockStore = configureMockStore(middlewares);
+
+describe('auth actions', () => {
+  const email = 'test@mail.com';
+  const password = 'password';
+  const token =
+    'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJBcGlBdXRoIiwic3ViIjoidGVzdEBtYWlsLmNvbSIsImRvbWFpbiI6IiIsImlhdCI6MTUyOTEzNzczODcyNSwiZXhwIjoxNTI5MTM3NzM4NzI1fQ.tdI7r11bmSCUmbcJBBKIDt7Hkb7POLMRl8VNJv_8O_s';
+
+  describe('#signupUser()', () => {
+    it('should dispatch SENT_VERIFICATION when signing up user has been done', done => {
+      nock('http://localhost')
+        .post('/api/auth/signup')
+        .reply(200, {
+          email,
+          message: 'Verification email has been sent.'
+        });
+
+      const store = mockStore({});
+
+      const expectedActions = [
+        { type: SIGNUP_LOADING },
+        {
+          type: SENT_VERIFICATION,
+          payload: email
+        }
+      ];
+
+      store
+        .dispatch(signupUser(email, password))
+        .then(() => {
+          expect(store.getActions()).to.deep.equal(expectedActions);
+          done();
+        })
+        .catch(error => done(error));
+    });
+  });
+
+  describe('#loginUser()', () => {
+    it('should dispatch AUTH_USER when logining user has been done', done => {
+      const pushStub = sinon.stub(Router, 'push');
+      pushStub.withArgs('/').returns('/');
+      const expectedRoute = '/';
+
+      nock('http://localhost')
+        .post('/api/auth/login')
+        .reply(200, {
+          token
+        });
+
+      const store = mockStore({});
+
+      const expectedActions = [
+        { type: LOGIN_LOADING },
+        { type: AUTH_RENEW },
+        {
+          type: AUTH_USER,
+          payload: email
+        },
+        {
+          type: SET_DOMAIN,
+          payload: ''
+        },
+        { type: SHOW_PAGE_LOADING }
+      ];
+
+      store
+        .dispatch(loginUser(email, password))
+        .then(() => {
+          expect(store.getActions()).to.deep.equal(expectedActions);
+
+          pushStub.restore();
+          sinon.assert.calledWith(pushStub, expectedRoute);
+
+          done();
+        })
+        .catch(error => done(error));
+    });
+  });
+
+  describe('#logoutUser()', () => {
+    it('should dispatch UNAUTH_USER when loging out user has been done', () => {
+      const pushStub = sinon.stub(Router, 'push');
+      pushStub.withArgs('/login').returns('/login');
+      const expectedRoute = '/login';
+
+      const store = mockStore({});
+
+      const expectedActions = [
+        { type: SHOW_PAGE_LOADING },
+        { type: UNAUTH_USER }
+      ];
+
+      store.dispatch(logoutUser());
+      expect(store.getActions()).to.deep.equal(expectedActions);
+
+      pushStub.restore();
+      sinon.assert.calledWith(pushStub, expectedRoute);
+    });
+  });
+
+  describe('#renewAuthUser()', () => {
+    it('should dispatch AUTH_RENEW when renewing auth user has been done', done => {
+      const cookieStub = sinon.stub(cookie, 'get');
+      cookieStub.withArgs('token').returns(token);
+
+      nock('http://localhost', {
+        reqheaders: {
+          Authorization: token
+        }
+      })
+        .post('/api/auth/renew')
+        .reply(200, {
+          token
+        });
+
+      const store = mockStore({ auth: { renew: false } });
+
+      const expectedActions = [
+        { type: AUTH_RENEW },
+        {
+          type: AUTH_USER,
+          payload: email
+        },
+        {
+          type: SET_DOMAIN,
+          payload: ''
+        }
+      ];
+
+      store
+        .dispatch(renewAuthUser())
+        .then(() => {
+          expect(store.getActions()).to.deep.equal(expectedActions);
+          cookieStub.restore();
+          done();
+        })
+        .catch(error => done(error));
+    });
+  });
+});

+ 165 - 0
client/actions/__test__/settings.js

@@ -0,0 +1,165 @@
+import nock from 'nock';
+import sinon from 'sinon';
+import { expect } from 'chai';
+import cookie from 'js-cookie';
+import thunk from 'redux-thunk';
+import configureMockStore from 'redux-mock-store';
+
+import {
+  getUserSettings,
+  setCustomDomain,
+  deleteCustomDomain,
+  generateApiKey
+} from '../settings';
+import {
+  DELETE_DOMAIN,
+  DOMAIN_LOADING,
+  API_LOADING,
+  SET_DOMAIN,
+  SET_APIKEY
+} from '../actionTypes';
+
+const middlewares = [thunk];
+const mockStore = configureMockStore(middlewares);
+
+describe('settings actions', () => {
+  const token =
+    'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJBcGlBdXRoIiwic3ViIjoidGVzdEBtYWlsLmNvbSIsImRvbWFpbiI6IiIsImlhdCI6MTUyOTEzNzczODcyNSwiZXhwIjoxNTI5MTM3NzM4NzI1fQ.tdI7r11bmSCUmbcJBBKIDt7Hkb7POLMRl8VNJv_8O_s';
+
+  let cookieStub;
+
+  beforeEach(() => {
+    cookieStub = sinon.stub(cookie, 'get');
+    cookieStub.withArgs('token').returns(token);
+  });
+
+  afterEach(() => {
+    cookieStub.restore();
+  });
+
+  describe('#getUserSettings()', () => {
+    it('should dispatch SET_APIKEY and SET_DOMAIN when getting user settings have been done', done => {
+      const apikey = '123';
+      const customDomain = 'test.com';
+
+      nock('http://localhost', {
+        reqheaders: {
+          Authorization: token
+        }
+      })
+        .get('/api/auth/usersettings')
+        .reply(200, { apikey, customDomain });
+
+      const store = mockStore({});
+
+      const expectedActions = [
+        {
+          type: SET_DOMAIN,
+          payload: customDomain
+        },
+        {
+          type: SET_APIKEY,
+          payload: apikey
+        }
+      ];
+
+      store
+        .dispatch(getUserSettings())
+        .then(() => {
+          expect(store.getActions()).to.deep.equal(expectedActions);
+          done();
+        })
+        .catch(error => done(error));
+    });
+  });
+
+  describe('#setCustomDomain()', () => {
+    it('should dispatch SET_DOMAIN when setting custom domain has been done', done => {
+      const customDomain = 'test.com';
+
+      nock('http://localhost', {
+        reqheaders: {
+          Authorization: token
+        }
+      })
+        .post('/api/url/customdomain')
+        .reply(200, { customDomain });
+
+      const store = mockStore({});
+
+      const expectedActions = [
+        { type: DOMAIN_LOADING },
+        {
+          type: SET_DOMAIN,
+          payload: customDomain
+        }
+      ];
+
+      store
+        .dispatch(setCustomDomain(customDomain))
+        .then(() => {
+          expect(store.getActions()).to.deep.equal(expectedActions);
+          done();
+        })
+        .catch(error => done(error));
+    });
+  });
+
+  describe('#deleteCustomDomain()', () => {
+    it('should dispatch DELETE_DOMAIN when deleting custom domain has been done', done => {
+      const customDomain = 'test.com';
+
+      nock('http://localhost', {
+        reqheaders: {
+          Authorization: token
+        }
+      })
+        .delete('/api/url/customdomain')
+        .reply(200, { customDomain });
+
+      const store = mockStore({});
+
+      const expectedActions = [{ type: DELETE_DOMAIN }];
+
+      store
+        .dispatch(deleteCustomDomain(customDomain))
+        .then(() => {
+          expect(store.getActions()).to.deep.equal(expectedActions);
+          done();
+        })
+        .catch(error => done(error));
+    });
+  });
+
+  describe('#generateApiKey()', () => {
+    it('should dispatch SET_APIKEY when generating api key has been done', done => {
+      const apikey = '123';
+
+      nock('http://localhost', {
+        reqheaders: {
+          Authorization: token
+        }
+      })
+        .post('/api/auth/generateapikey')
+        .reply(200, { apikey });
+
+      const store = mockStore({});
+
+      const expectedActions = [
+        { type: API_LOADING },
+        {
+          type: SET_APIKEY,
+          payload: apikey
+        }
+      ];
+
+      store
+        .dispatch(generateApiKey())
+        .then(() => {
+          expect(store.getActions()).to.deep.equal(expectedActions);
+          done();
+        })
+        .catch(error => done(error));
+    });
+  });
+});

+ 159 - 0
client/actions/__test__/url.js

@@ -0,0 +1,159 @@
+import nock from 'nock';
+import sinon from 'sinon';
+import { expect } from 'chai';
+import cookie from 'js-cookie';
+import thunk from 'redux-thunk';
+import configureMockStore from 'redux-mock-store';
+
+import { createShortUrl, getUrlsList, deleteShortUrl } from '../url';
+import { ADD_URL, LIST_URLS, DELETE_URL, TABLE_LOADING } from '../actionTypes';
+
+const middlewares = [thunk];
+const mockStore = configureMockStore(middlewares);
+
+describe('url actions', () => {
+  const token =
+    'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJBcGlBdXRoIiwic3ViIjoidGVzdEBtYWlsLmNvbSIsImRvbWFpbiI6IiIsImlhdCI6MTUyOTEzNzczODcyNSwiZXhwIjoxNTI5MTM3NzM4NzI1fQ.tdI7r11bmSCUmbcJBBKIDt7Hkb7POLMRl8VNJv_8O_s';
+
+  let cookieStub;
+
+  beforeEach(() => {
+    cookieStub = sinon.stub(cookie, 'get');
+    cookieStub.withArgs('token').returns(token);
+  });
+
+  afterEach(() => {
+    cookieStub.restore();
+  });
+
+  describe('#createShortUrl()', () => {
+    it('should dispatch ADD_URL when creating short url has been done', done => {
+      const url = 'test.com';
+      const mockedItems = {
+        createdAt: '2018-06-16T15:40:35.243Z',
+        id: '123',
+        target: url,
+        password: false,
+        reuse: false,
+        shortUrl: 'http://kutt.it/123'
+      };
+
+      nock('http://localhost', {
+        reqheaders: {
+          Authorization: token
+        }
+      })
+        .post('/api/url/submit')
+        .reply(200, mockedItems);
+
+      const store = mockStore({});
+
+      const expectedActions = [
+        {
+          type: ADD_URL,
+          payload: mockedItems
+        }
+      ];
+
+      store
+        .dispatch(createShortUrl(url))
+        .then(() => {
+          expect(store.getActions()).to.deep.equal(expectedActions);
+          done();
+        })
+        .catch(error => done(error));
+    });
+  });
+
+  describe('#getUrlsList()', () => {
+    it('should dispatch LIST_URLS when getting urls list has been done', done => {
+      const mockedQueryParams = {
+        isShortened: false,
+        count: 10,
+        countAll: 1,
+        page: 1,
+        search: ''
+      };
+
+      const mockedItems = {
+        list: [
+          {
+            createdAt: '2018-06-16T16:45:28.607Z',
+            id: 'UkEs33',
+            target: 'https://kutt.it/',
+            password: false,
+            count: 0,
+            shortUrl: 'http://test.com/UkEs33'
+          }
+        ],
+        countAll: 1
+      };
+
+      nock('http://localhost', {
+        reqheaders: {
+          Authorization: token
+        }
+      })
+        .get('/api/url/geturls')
+        .query(mockedQueryParams)
+        .reply(200, mockedItems);
+
+      const store = mockStore({ url: { list: [], ...mockedQueryParams } });
+
+      const expectedActions = [
+        { type: TABLE_LOADING },
+        {
+          type: LIST_URLS,
+          payload: mockedItems
+        }
+      ];
+
+      store
+        .dispatch(getUrlsList())
+        .then(() => {
+          expect(store.getActions()).to.deep.equal(expectedActions);
+          done();
+        })
+        .catch(error => done(error));
+    });
+  });
+
+  describe('#deleteShortUrl()', () => {
+    it('should dispatch DELETE_URL when deleting short url has been done', done => {
+      const id = '123';
+      const mockedItems = [
+        {
+          createdAt: '2018-06-16T15:40:35.243Z',
+          id: '123',
+          target: 'test.com',
+          password: false,
+          reuse: false,
+          shortUrl: 'http://kutt.it/123'
+        }
+      ];
+
+      nock('http://localhost', {
+        reqheaders: {
+          Authorization: token
+        }
+      })
+        .post('/api/url/deleteurl')
+        .reply(200, { message: 'Sort URL deleted successfully' });
+
+      const store = mockStore({ url: { list: mockedItems } });
+
+      const expectedActions = [
+        { type: TABLE_LOADING },
+        { type: DELETE_URL, payload: id }
+      ];
+
+      store
+        .dispatch(deleteShortUrl({ id }))
+        .then(() => {
+          expect(store.getActions()).to.deep.equal(expectedActions);
+          done();
+        })
+        .catch(error => done(error));
+    });
+  });
+});