From a4ba4d4d98d6faa57616f09bc1b179cc03e3e290 Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Mon, 20 Jan 2020 10:11:38 +0800 Subject: [PATCH] 795th Commit --- package.json | 1 + .../crud-operations/rest/__mocks__/axios.js | 8 -- .../rest/__tests__/actions.spec.js | 97 +++++++------------ yarn.lock | 7 ++ 4 files changed, 43 insertions(+), 70 deletions(-) delete mode 100644 src/shell/crud-operations/rest/__mocks__/axios.js diff --git a/package.json b/package.json index 119c1a22..f6e9377b 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,7 @@ "@storybook/addons": "5.2.8", "@storybook/vue": "5.2.8", "@vue/test-utils": "1.0.0-beta.30", + "axios-mock-adapter": "1.17.0", "babel-eslint": "8.2.6", "babel-jest": "24.8.0", "babel-loader": "8.0.5", diff --git a/src/shell/crud-operations/rest/__mocks__/axios.js b/src/shell/crud-operations/rest/__mocks__/axios.js deleted file mode 100644 index e3cdc699..00000000 --- a/src/shell/crud-operations/rest/__mocks__/axios.js +++ /dev/null @@ -1,8 +0,0 @@ -const axios = { - post: () => new Promise(res => res({ message: 'Data saved.' })), - get: () => new Promise(res => res({ data: [{ id: 'vn3RecDbwMQTjttnluZW', text: 'qaz123' }] })), - put: () => new Promise(res => res({ message: 'Data updated.' })), - delete: () => new Promise(res => res({ message: 'Data deleted.' })), -}; - -export default axios; diff --git a/src/shell/crud-operations/rest/__tests__/actions.spec.js b/src/shell/crud-operations/rest/__tests__/actions.spec.js index 63addf98..d39911da 100644 --- a/src/shell/crud-operations/rest/__tests__/actions.spec.js +++ b/src/shell/crud-operations/rest/__tests__/actions.spec.js @@ -1,7 +1,10 @@ -import { INITIAL } from '../constants'; +import axios from 'axios'; +import AxiosMockAdapter from 'axios-mock-adapter'; + +import { API_LIST, INITIAL } from '../constants'; import actions from '../actions'; -jest.mock('axios'); +const mockAxios = new AxiosMockAdapter(axios); describe('actions', () => { it('should handle success', () => { @@ -26,30 +29,22 @@ describe('actions', () => { actions.failure({ commit }, error); }); - it('should handle addItem (success)', (done) => { - const dispatch = (type) => { - expect(type).toBe('searchItem'); - done(); - }; + it('should handle addItem (success)', async () => { + mockAxios.onPost(API_LIST).reply(200, { message: 'Data saved.' }); + const dispatch = (type) => { expect(type).toBe('searchItem'); }; const state = { ...INITIAL }; - const payload = 'qaz123'; - actions.addItem({ dispatch, state }, payload); + await actions.addItem({ dispatch, state }, payload); }); - it('should handle addItem (failure)', (done) => { - const dispatch = (type) => { - expect(type).toBe('failure'); - done(); - }; - + it('should handle addItem (failure)', async () => { + const dispatch = (type) => { expect(type).toBe('failure'); }; const state = { ...INITIAL }; - const payload = new Error('Error!'); - actions.addItem({ dispatch, state }, payload); + await actions.addItem({ dispatch, state }, payload); }); it('should handle addItem (undefined)', async () => { @@ -58,86 +53,64 @@ describe('actions', () => { const payload = ''; const result = await actions.addItem({ dispatch, state }, payload); - expect(result).toBeUndefined(); }); - it('should handle searchItem (success)', (done) => { + it('should handle searchItem (success)', async () => { + mockAxios.onGet(API_LIST).reply(200, { data: [{ id: 'vn3RecDbwMQTjttnluZW', text: 'qaz123' }] }); + const dispatch = (type, payload) => { expect(type).toBe('success'); - expect(payload).toEqual([{ id: 'vn3RecDbwMQTjttnluZW', text: 'qaz123' }]); - done(); + expect(payload).toEqual({ data: [{ id: 'vn3RecDbwMQTjttnluZW', text: 'qaz123' }] }); }; const state = { ...INITIAL }; - const payload = ''; - actions.searchItem({ dispatch, state }, payload); + await actions.searchItem({ dispatch, state }, payload); }); - it('should handle searchItem (failure)', (done) => { - const dispatch = (type) => { - expect(type).toBe('failure'); - done(); - }; - + it('should handle searchItem (failure)', async () => { + const dispatch = (type) => { expect(type).toBe('failure'); }; const state = { ...INITIAL }; - const payload = ''; - actions.searchItem({ dispatch, state }, payload); + await actions.searchItem({ dispatch, state }, payload); }); - it('should handle editItem (success)', (done) => { - const dispatch = (type) => { - expect(type).toBe('searchItem'); - done(); - }; + it('should handle editItem (success)', async () => { + mockAxios.onPut(`${API_LIST}/vn3RecDbwMQTjttnluZW`).reply(200, { message: 'Data updated.' }); + const dispatch = (type) => { expect(type).toBe('searchItem'); }; const state = { ...INITIAL }; + const payload = { id: 'vn3RecDbwMQTjttnluZW', text: 'zxc123' }; - const payload = { id: 'vn3RecDbwMQTjttnluZW', text: 'qaz123' }; - - actions.editItem({ dispatch, state }, payload); + await actions.editItem({ dispatch, state }, payload); }); - it('should handle editItem (failure)', (done) => { - const dispatch = (type) => { - expect(type).toBe('failure'); - done(); - }; - + it('should handle editItem (failure)', async () => { + const dispatch = (type) => { expect(type).toBe('failure'); }; const state = { ...INITIAL }; - const payload = { id: 'vn3RecDbwMQTjttnluZW', text: new Error('Error!') }; - actions.editItem({ dispatch, state }, payload); + await actions.editItem({ dispatch, state }, payload); }); - it('should handle deleteItem (success)', (done) => { - const dispatch = (type) => { - expect(type).toBe('searchItem'); - done(); - }; + it('should handle deleteItem (success)', async () => { + mockAxios.onDelete(`${API_LIST}/vn3RecDbwMQTjttnluZW`).reply(200, { message: 'Data deleted.' }); + const dispatch = (type) => { expect(type).toBe('searchItem'); }; const state = { ...INITIAL }; - const payload = 'vn3RecDbwMQTjttnluZW'; - actions.deleteItem({ dispatch, state }, payload); + await actions.deleteItem({ dispatch, state }, payload); }); - it('should handle deleteItem (failure)', (done) => { - const dispatch = (type) => { - expect(type).toBe('failure'); - done(); - }; - + it('should handle deleteItem (failure)', async () => { + const dispatch = (type) => { expect(type).toBe('failure'); }; const state = { ...INITIAL }; - const payload = null; - actions.deleteItem({ dispatch, state }, payload); + await actions.deleteItem({ dispatch, state }, payload); }); }); diff --git a/yarn.lock b/yarn.lock index 79ec90ab..ef5143ee 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2598,6 +2598,13 @@ aws4@^1.8.0: resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.9.0.tgz#24390e6ad61386b0a747265754d2a17219de862c" integrity sha512-Uvq6hVe90D0B2WEnUqtdgY1bATGz3mw33nH9Y+dmA+w5DHvUmBgkr5rM/KCHpCsiFNRUfokW/szpPPgMK2hm4A== +axios-mock-adapter@1.17.0: + version "1.17.0" + resolved "https://registry.yarnpkg.com/axios-mock-adapter/-/axios-mock-adapter-1.17.0.tgz#0dbee43c606d4aaba5a43d88d96d6661a7cc3c04" + integrity sha512-q3efmwJUOO4g+wsLNSk9Ps1UlJoF3fQ3FSEe4uEEhkRtu7SoiAVPj8R3Hc/WP55MBTVFzaDP9QkdJhdVhP8A1Q== + dependencies: + deep-equal "^1.0.1" + axios@0.19.0: version "0.19.0" resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.0.tgz#8e09bff3d9122e133f7b8101c8fbdd00ed3d2ab8"