Преглед на файлове

Added tests and update ignore rule

Oleh T преди 7 години
родител
ревизия
9946616689
променени са 6 файла, в които са добавени 716 реда и са изтрити 1 реда
  1. 2 1
      .eslintignore
  2. 82 0
      client/reducers/__test__/auth.js
  3. 134 0
      client/reducers/__test__/error.js
  4. 211 0
      client/reducers/__test__/loading.js
  5. 90 0
      client/reducers/__test__/settings.js
  6. 197 0
      client/reducers/__test__/url.js

+ 2 - 1
.eslintignore

@@ -1,3 +1,4 @@
 .next/
 flow-typed/
-node_modules/
+node_modules/
+client/**/__test__/

+ 82 - 0
client/reducers/__test__/auth.js

@@ -0,0 +1,82 @@
+import { expect } from 'chai';
+import deepFreeze from 'deep-freeze';
+
+import {
+  AUTH_USER,
+  AUTH_RENEW,
+  UNAUTH_USER,
+  SENT_VERIFICATION
+} from '../../actions/actionTypes';
+
+import reducer from '../auth';
+
+describe('auth reducer', () => {
+  const initialState = {
+    isAuthenticated: false,
+    sentVerification: false,
+    user: '',
+    renew: false
+  };
+
+  beforeEach(() => {
+    deepFreeze(initialState);
+  });
+
+  it('should return the initial state', () => {
+    expect(reducer(undefined, {})).to.deep.equal(initialState);
+  });
+
+  it('should handle AUTH_USER', () => {
+    const user = 'test@user.com';
+
+    const state = reducer(initialState, {
+      type: AUTH_USER,
+      payload: user
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.isAuthenticated).to.be.true;
+    expect(state.user).to.be.equal(user);
+    expect(state.sentVerification).to.be.false;
+  });
+
+  it('should handle AUTH_RENEW', () => {
+    const state = reducer(initialState, {
+      type: AUTH_RENEW
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.renew).to.be.true;
+  });
+
+  it('should handle UNAUTH_USER', () => {
+    const state = reducer(initialState, {
+      type: UNAUTH_USER
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state).to.deep.equal(initialState);
+  });
+
+  it('should handle SENT_VERIFICATION', () => {
+    const user = 'test@user.com';
+
+    const state = reducer(initialState, {
+      type: SENT_VERIFICATION,
+      payload: user
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.sentVerification).to.be.true;
+    expect(state.user).to.be.equal(user);
+  });
+
+  it('should not handle other action types', () => {
+    const state = reducer(initialState, {
+      type: 'ANOTHER_ACTION'
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state).to.deep.equal(initialState);
+  });
+});

+ 134 - 0
client/reducers/__test__/error.js

@@ -0,0 +1,134 @@
+import { expect } from 'chai';
+import deepFreeze from 'deep-freeze';
+
+import {
+  SHORTENER_ERROR,
+  DOMAIN_ERROR,
+  SET_DOMAIN,
+  SHOW_DOMAIN_INPUT,
+  ADD_URL,
+  UPDATE_URL,
+  AUTH_ERROR,
+  AUTH_USER,
+  HIDE_PAGE_LOADING
+} from '../../actions/actionTypes';
+
+import reducer from '../error';
+
+describe('error reducer', () => {
+  const initialState = {
+    auth: '',
+    domain: '',
+    shortener: '',
+    urlOptions: ''
+  };
+
+  beforeEach(() => {
+    deepFreeze(initialState);
+  });
+
+  it('should return the initial state', () => {
+    expect(reducer(undefined, {})).to.deep.equal(initialState);
+  });
+
+  it('should handle SHORTENER_ERROR', () => {
+    const error = 'SHORTENER_ERROR';
+
+    const state = reducer(initialState, {
+      type: SHORTENER_ERROR,
+      payload: error
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.shortener).to.be.equal(error);
+  });
+
+  it('should handle DOMAIN_ERROR', () => {
+    const error = 'DOMAIN_ERROR';
+
+    const state = reducer(initialState, {
+      type: DOMAIN_ERROR,
+      payload: error
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.domain).to.be.equal(error);
+  });
+
+  it('should handle SET_DOMAIN', () => {
+    const state = reducer(initialState, {
+      type: SET_DOMAIN
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.domain).to.be.empty;
+  });
+
+  it('should handle SHOW_DOMAIN_INPUT', () => {
+    const state = reducer(initialState, {
+      type: SHOW_DOMAIN_INPUT
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.domain).to.be.empty;
+  });
+
+  it('should handle ADD_URL', () => {
+    const state = reducer(initialState, {
+      type: ADD_URL
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.shortener).to.be.empty;
+  });
+
+  it('should handle UPDATE_URL', () => {
+    const state = reducer(initialState, {
+      type: UPDATE_URL
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.urlOptions).to.be.empty;
+  });
+
+  it('should handle AUTH_ERROR', () => {
+    const error = 'AUTH_ERROR';
+
+    const state = reducer(initialState, {
+      type: AUTH_ERROR,
+      payload: error
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.auth).to.be.equal(error);
+  });
+
+  it('should handle AUTH_USER', () => {
+    const state = reducer(initialState, {
+      type: AUTH_USER
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.auth).to.be.empty;
+  });
+
+  it('should handle HIDE_PAGE_LOADING', () => {
+    const state = reducer(initialState, {
+      type: HIDE_PAGE_LOADING
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.auth).to.be.empty;
+    expect(state.shortener).to.be.empty;
+    expect(state.urlOptions).to.be.empty;
+  });
+
+  it('should not handle other action types', () => {
+    const state = reducer(initialState, {
+      type: 'ANOTHER_ACTION'
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state).to.deep.equal(initialState);
+  });
+});

+ 211 - 0
client/reducers/__test__/loading.js

@@ -0,0 +1,211 @@
+import { expect } from 'chai';
+import deepFreeze from 'deep-freeze';
+
+import {
+  SHOW_PAGE_LOADING,
+  HIDE_PAGE_LOADING,
+  TABLE_LOADING,
+  LOGIN_LOADING,
+  SIGNUP_LOADING,
+  SHORTENER_LOADING,
+  ADD_URL,
+  SHORTENER_ERROR,
+  LIST_URLS,
+  DELETE_URL,
+  AUTH_ERROR,
+  AUTH_USER,
+  DOMAIN_LOADING,
+  SET_DOMAIN,
+  DOMAIN_ERROR,
+  API_LOADING,
+  SET_APIKEY
+} from '../../actions/actionTypes';
+
+import reducer from '../loading';
+
+describe('loading reducer', () => {
+  const initialState = {
+    api: false,
+    domain: false,
+    shortener: false,
+    login: false,
+    page: false,
+    table: false,
+    signup: false
+  };
+
+  beforeEach(() => {
+    deepFreeze(initialState);
+  });
+
+  it('should return the initial state', () => {
+    expect(reducer(undefined, {})).to.deep.equal(initialState);
+  });
+
+  it('should handle SHOW_PAGE_LOADING', () => {
+    const state = reducer(initialState, {
+      type: SHOW_PAGE_LOADING
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.page).to.be.true;
+  });
+
+  it('should handle HIDE_PAGE_LOADING', () => {
+    const state = reducer(initialState, {
+      type: HIDE_PAGE_LOADING
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.shortener).to.be.false;
+    expect(state.login).to.be.false;
+    expect(state.page).to.be.false;
+    expect(state.signup).to.be.false;
+  });
+
+  it('should handle TABLE_LOADING', () => {
+    const state = reducer(initialState, {
+      type: TABLE_LOADING
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.table).to.be.true;
+  });
+
+  it('should handle LOGIN_LOADING', () => {
+    const state = reducer(initialState, {
+      type: LOGIN_LOADING
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.login).to.be.true;
+  });
+
+  it('should handle SIGNUP_LOADING', () => {
+    const state = reducer(initialState, {
+      type: SIGNUP_LOADING
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.signup).to.be.true;
+  });
+
+  it('should handle SHORTENER_LOADING', () => {
+    const state = reducer(initialState, {
+      type: SHORTENER_LOADING
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.shortener).to.be.true;
+  });
+
+  it('should handle ADD_URL', () => {
+    const state = reducer(initialState, {
+      type: ADD_URL
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.shortener).to.be.false;
+  });
+
+  it('should handle SHORTENER_ERROR', () => {
+    const state = reducer(initialState, {
+      type: SHORTENER_ERROR
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.shortener).to.be.false;
+  });
+
+  it('should handle LIST_URLS', () => {
+    const state = reducer(initialState, {
+      type: LIST_URLS
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.table).to.be.false;
+  });
+
+  it('should handle DELETE_URL', () => {
+    const state = reducer(initialState, {
+      type: DELETE_URL
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.table).to.be.false;
+  });
+
+  it('should handle AUTH_ERROR', () => {
+    const state = reducer(initialState, {
+      type: AUTH_ERROR
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.login).to.be.false;
+    expect(state.signup).to.be.false;
+  });
+
+  it('should handle AUTH_USER', () => {
+    const state = reducer(initialState, {
+      type: AUTH_USER
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.login).to.be.false;
+    expect(state.signup).to.be.false;
+  });
+
+  it('should handle DOMAIN_LOADING', () => {
+    const state = reducer(initialState, {
+      type: DOMAIN_LOADING
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.domain).to.be.true;
+  });
+
+  it('should handle SET_DOMAIN', () => {
+    const state = reducer(initialState, {
+      type: SET_DOMAIN
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.domain).to.be.false;
+  });
+
+  it('should handle DOMAIN_ERROR', () => {
+    const state = reducer(initialState, {
+      type: DOMAIN_ERROR
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.domain).to.be.false;
+  });
+
+  it('should handle API_LOADING', () => {
+    const state = reducer(initialState, {
+      type: API_LOADING
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.api).to.be.true;
+  });
+
+  it('should handle SET_APIKEY', () => {
+    const state = reducer(initialState, {
+      type: SET_APIKEY
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.api).to.be.false;
+  });
+
+  it('should not handle other action types', () => {
+    const state = reducer(initialState, {
+      type: 'ANOTHER_ACTION'
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state).to.deep.equal(initialState);
+  });
+});

+ 90 - 0
client/reducers/__test__/settings.js

@@ -0,0 +1,90 @@
+import { expect } from 'chai';
+import deepFreeze from 'deep-freeze';
+
+import {
+  SET_DOMAIN,
+  SET_APIKEY,
+  DELETE_DOMAIN,
+  SHOW_DOMAIN_INPUT,
+  UNAUTH_USER
+} from '../../actions/actionTypes';
+
+import reducer from '../settings';
+
+describe('settings reducer', () => {
+  const initialState = {
+    apikey: '',
+    customDomain: '',
+    domainInput: true
+  };
+
+  beforeEach(() => {
+    deepFreeze(initialState);
+  });
+
+  it('should return the initial state', () => {
+    expect(reducer(undefined, {})).to.deep.equal(initialState);
+  });
+
+  it('should handle SET_DOMAIN', () => {
+    const domain = 'example.com';
+
+    const state = reducer(initialState, {
+      type: SET_DOMAIN,
+      payload: domain
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.customDomain).to.be.equal(domain);
+    expect(state.domainInput).to.be.false;
+  });
+
+  it('should handle SET_APIKEY', () => {
+    const apikey = '1234567';
+
+    const state = reducer(initialState, {
+      type: SET_APIKEY,
+      payload: apikey
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.apikey).to.be.equal(apikey);
+  });
+
+  it('should handle DELETE_DOMAIN', () => {
+    const state = reducer(initialState, {
+      type: DELETE_DOMAIN
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.customDomain).to.be.empty;
+    expect(state.domainInput).to.be.true;
+  });
+
+  it('should handle SHOW_DOMAIN_INPUT', () => {
+    const state = reducer(initialState, {
+      type: SHOW_DOMAIN_INPUT
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state.domainInput).to.be.true;
+  });
+
+  it('should handle UNAUTH_USER', () => {
+    const state = reducer(initialState, {
+      type: UNAUTH_USER
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state).to.deep.equal(initialState);
+  });
+
+  it('should not handle other action types', () => {
+    const state = reducer(initialState, {
+      type: 'ANOTHER_ACTION'
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state).to.deep.equal(initialState);
+  });
+});

+ 197 - 0
client/reducers/__test__/url.js

@@ -0,0 +1,197 @@
+import { expect } from 'chai';
+import deepFreeze from 'deep-freeze';
+
+import {
+  ADD_URL,
+  UPDATE_URL_LIST,
+  LIST_URLS,
+  DELETE_URL,
+  UNAUTH_USER
+} from '../../actions/actionTypes';
+
+import reducer from '../url';
+
+describe('url reducer', () => {
+  const initialState = {
+    list: [],
+    isShortened: false,
+    count: 10,
+    countAll: 0,
+    page: 1,
+    search: ''
+  };
+
+  beforeEach(() => {
+    deepFreeze(initialState);
+  });
+
+  it('should return the initial state', () => {
+    expect(reducer(undefined, {})).to.deep.equal(initialState);
+  });
+
+  it('should handle ADD_URL', () => {
+    const item = {
+      createdAt: '2018-06-12T19:23:00.272Z',
+      id: 'YufjdS',
+      target: 'https://kutt.it/',
+      password: false,
+      reuse: false,
+      shortUrl: 'https://kutt.it/YufjdS'
+    };
+
+    const state = reducer(initialState, {
+      type: ADD_URL,
+      payload: item
+    });
+
+    expect(state.list).to.be.an('array');
+    expect(state.list).to.have.lengthOf(1);
+    expect(state.list).to.include(item);
+    expect(state.isShortened).to.be.true;
+  });
+
+  it('should handle UPDATE_URL_LIST', () => {
+    const count = 10;
+    const page = 1;
+    const search = 'test url';
+
+    const allParamsState = reducer(initialState, {
+      type: UPDATE_URL_LIST,
+      payload: { count, page, search }
+    });
+
+    expect(allParamsState).not.to.be.undefined;
+    expect(allParamsState.count).to.be.equal(count);
+    expect(allParamsState.page).to.be.equal(page);
+    expect(allParamsState.search).to.be.equal(search);
+
+    const countState = reducer(initialState, {
+      type: UPDATE_URL_LIST,
+      payload: { count }
+    });
+
+    expect(countState).not.to.be.undefined;
+    expect(countState.count).to.be.equal(count);
+
+    const pageState = reducer(initialState, {
+      type: UPDATE_URL_LIST,
+      payload: { page }
+    });
+
+    expect(pageState).not.to.be.undefined;
+    expect(pageState.page).to.be.equal(page);
+
+    const searchState = reducer(initialState, {
+      type: UPDATE_URL_LIST,
+      payload: { search }
+    });
+
+    expect(searchState).not.to.be.undefined;
+    expect(searchState.search).to.be.equal(search);
+
+    const state = reducer(initialState, {
+      type: UPDATE_URL_LIST
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state).to.deep.equal(initialState);
+  });
+
+  it('should handle LIST_URLS', () => {
+    const list = [
+      {
+        createdAt: '2018-06-12T19:23:00.272Z',
+        id: 'YufjdS',
+        target: 'https://kutt.it/',
+        password: false,
+        reuse: false,
+        shortUrl: 'https://kutt.it/YufjdS'
+      },
+      {
+        createdAt: '2018-06-12T19:51:56.435Z',
+        id: '1gCdbC',
+        target: 'https://kutt.it/',
+        password: false,
+        reuse: false,
+        shortUrl: 'https://kutt.it/1gCdbC'
+      }
+    ];
+
+    const countAll = list.length;
+
+    const state = reducer(initialState, {
+      type: LIST_URLS,
+      payload: { list, countAll }
+    });
+
+    expect(state.list).to.be.an('array');
+    expect(state.list).to.have.lengthOf(2);
+    expect(state.list).to.not.include(list);
+    expect(state.countAll).to.be.equal(countAll);
+    expect(state.isShortened).to.be.false;
+  });
+
+  it('should handle DELETE_URL', () => {
+    const itemsState = {
+      list: [
+        {
+          createdAt: '2018-06-12T19:23:00.272Z',
+          id: 'YufjdS',
+          target: 'https://kutt.it/',
+          password: false,
+          reuse: false,
+          shortUrl: 'https://kutt.it/YufjdS'
+        },
+        {
+          createdAt: '2018-06-12T19:51:56.435Z',
+          id: '1gCdbC',
+          target: 'https://kutt.it/',
+          password: false,
+          reuse: false,
+          shortUrl: 'https://kutt.it/1gCdbC'
+        }
+      ],
+      isShortened: true,
+      count: 10,
+      countAll: 2,
+      page: 1,
+      search: ''
+    };
+
+    deepFreeze(itemsState);
+
+    const state = reducer(itemsState, {
+      type: DELETE_URL,
+      payload: 'YufjdS'
+    });
+
+    expect(state.list).to.be.an('array');
+    expect(state.list).to.have.lengthOf(1);
+    expect(state.list).to.not.include({
+      createdAt: '2018-06-12T19:23:00.272Z',
+      id: 'YufjdS',
+      target: 'https://kutt.it/',
+      password: false,
+      reuse: false,
+      shortUrl: 'https://kutt.it/YufjdS'
+    });
+  });
+
+  it('should handle UNAUTH_USER', () => {
+    const state = reducer(initialState, {
+      type: UNAUTH_USER
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state).to.deep.equal(initialState);
+  });
+
+  it('should not handle other action types', () => {
+    const state = reducer(initialState, {
+      type: 'ANOTHER_ACTION'
+    });
+
+    expect(state).not.to.be.undefined;
+    expect(state).to.deep.equal(initialState);
+  });
+});