From 177663f91d4952d277b59164ec080af2966ad9a8 Mon Sep 17 00:00:00 2001 From: kskibinski Date: Fri, 11 Oct 2019 11:42:26 +0200 Subject: [PATCH] Add unit tests for 'core/modules/mailer' --- .eslintrc.js | 1 + CHANGELOG.md | 3 ++ .../mailer/test/unit/sendEmail.spec.ts | 47 +++++++++++++++++++ package.json | 6 ++- test/unit/jest.conf.js | 2 +- test/unit/setupTestEnvironment.js | 1 - test/unit/setupTestEnvironment.ts | 6 +++ yarn.lock | 31 ++++++++++++ 8 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 core/modules/mailer/test/unit/sendEmail.spec.ts delete mode 100644 test/unit/setupTestEnvironment.js create mode 100644 test/unit/setupTestEnvironment.ts diff --git a/.eslintrc.js b/.eslintrc.js index 2d98134c4d..3697d18fb2 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,6 +1,7 @@ module.exports = { root: true, env: { browser: true, jest: true }, + globals: { fetchMock: true }, parser: 'vue-eslint-parser', parserOptions: { parser: '@typescript-eslint/parser', diff --git a/CHANGELOG.md b/CHANGELOG.md index 26c6f2bc5a..a4ead8809b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +### Added + +- Added unit tests for `core/modules/mailer` - @krskibin (#3710) ## [1.11.0-rc.1] - 2019.10.03 diff --git a/core/modules/mailer/test/unit/sendEmail.spec.ts b/core/modules/mailer/test/unit/sendEmail.spec.ts new file mode 100644 index 0000000000..a36001b3e8 --- /dev/null +++ b/core/modules/mailer/test/unit/sendEmail.spec.ts @@ -0,0 +1,47 @@ +import { mailerStore } from '../../store/index' +import config from 'config' + +jest.mock('@vue-storefront/i18n', () => ({ t: jest.fn(str => str) })); +jest.mock('@vue-storefront/core/lib/storage-manager', () => jest.fn()) +jest.mock('@vue-storefront/core/app', () => jest.fn()) +jest.mock('@vue-storefront/core/lib/multistore', () => jest.fn()) +jest.mock('@vue-storefront/core/store', () => ({ Module: jest.fn() })) +jest.mock('@vue-storefront/core/lib/logger', () => ({ + Logger: { + error: jest.fn(() => jest.fn()) + } +})) + +describe('Mailer store module', () => { + const letterMock = {} + const contextMock = {}; + const wrapper = (actions: any) => actions.sendEmail(contextMock, letterMock) + + beforeEach(() => { + jest.clearAllMocks(); + fetchMock.resetMocks() + }) + + it('should send email succesfully', async () => { + fetchMock.mockResponses( + [ JSON.stringify({ code: 200 }), { status: 200 } ], + [ JSON.stringify({ send: true }), { status: 200 } ] + ) + + const res = await wrapper(mailerStore.actions); + const resData = await res.json() + + expect(resData.send).toBe(true) + }) + + it('should thrown error when response code is wrong', async () => { + const wrongResponseCode = 201; + fetchMock.mockResponseOnce(JSON.stringify({ code: wrongResponseCode })) + + try { + const res = await wrapper(mailerStore.actions) + } catch (e) { + expect(e.message).toBe(`Error: ${wrongResponseCode}`) + } + }) +}) diff --git a/package.json b/package.json index 7421865ff4..9987f7098e 100755 --- a/package.json +++ b/package.json @@ -50,7 +50,10 @@ }, "lint-staged": { "*.{js,vue,ts}": "eslint", - "**/i18n/*.csv": ["node ./core/scripts/utils/sort-translations.js", "git add"] + "**/i18n/*.csv": [ + "node ./core/scripts/utils/sort-translations.js", + "git add" + ] }, "husky": { "hooks": { @@ -145,6 +148,7 @@ "inquirer": "^3.3.0", "is-windows": "^1.0.1", "jest": "^24.8.0", + "jest-fetch-mock": "^2.1.2", "jest-serializer-vue": "^2.0.2", "jsonfile": "^4.0.0", "lerna": "^3.14.1", diff --git a/test/unit/jest.conf.js b/test/unit/jest.conf.js index 6a11736922..dadde07caf 100644 --- a/test/unit/jest.conf.js +++ b/test/unit/jest.conf.js @@ -36,6 +36,6 @@ module.exports = { '/node_modules/(?!lodash-es/.*)' ], setupFiles: [ - '/test/unit/setupTestEnvironment.js' + '/test/unit/setupTestEnvironment.ts' ] } diff --git a/test/unit/setupTestEnvironment.js b/test/unit/setupTestEnvironment.js deleted file mode 100644 index 33b7c42cf4..0000000000 --- a/test/unit/setupTestEnvironment.js +++ /dev/null @@ -1 +0,0 @@ -import '@babel/polyfill' diff --git a/test/unit/setupTestEnvironment.ts b/test/unit/setupTestEnvironment.ts new file mode 100644 index 0000000000..a225c14563 --- /dev/null +++ b/test/unit/setupTestEnvironment.ts @@ -0,0 +1,6 @@ +import '@babel/polyfill' +import {GlobalWithFetchMock} from 'jest-fetch-mock'; + +const customGlobal: GlobalWithFetchMock = global as GlobalWithFetchMock; +customGlobal.fetch = require('jest-fetch-mock'); +customGlobal.fetchMock = customGlobal.fetch; diff --git a/yarn.lock b/yarn.lock index 3de0ff47e4..0bac2b2700 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4943,6 +4943,14 @@ cross-env@^3.1.4: cross-spawn "^5.1.0" is-windows "^1.0.0" +cross-fetch@^2.2.2: + version "2.2.3" + resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-2.2.3.tgz#e8a0b3c54598136e037f8650f8e823ccdfac198e" + integrity sha512-PrWWNH3yL2NYIb/7WF/5vFG3DCQiXDOVf8k3ijatbrtnwNuhMWLC7YF7uqf53tbTFDzHIUD8oITw4Bxt8ST3Nw== + dependencies: + node-fetch "2.1.2" + whatwg-fetch "2.0.4" + cross-spawn@^3.0.0: version "3.0.1" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-3.0.1.tgz#1256037ecb9f0c5f79e3d6ef135e30770184b982" @@ -8128,6 +8136,14 @@ jest-environment-node@^24.8.0: jest-mock "^24.8.0" jest-util "^24.8.0" +jest-fetch-mock@^2.1.2: + version "2.1.2" + resolved "https://registry.yarnpkg.com/jest-fetch-mock/-/jest-fetch-mock-2.1.2.tgz#1260b347918e3931c4ec743ceaf60433da661bd0" + integrity sha512-tcSR4Lh2bWLe1+0w/IwvNxeDocMI/6yIA2bijZ0fyWxC4kQ18lckQ1n7Yd40NKuisGmcGBRFPandRXrW/ti/Bw== + dependencies: + cross-fetch "^2.2.2" + promise-polyfill "^7.1.1" + jest-get-type@^24.8.0: version "24.8.0" resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-24.8.0.tgz#a7440de30b651f5a70ea3ed7ff073a32dfe646fc" @@ -9815,6 +9831,11 @@ node-fetch-npm@^2.0.2: json-parse-better-errors "^1.0.0" safe-buffer "^5.1.1" +node-fetch@2.1.2: + version "2.1.2" + resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.1.2.tgz#ab884e8e7e57e38a944753cec706f788d1768bb5" + integrity sha1-q4hOjn5X44qUR1POxwb3iNF2i7U= + node-fetch@^1.0.1: version "1.7.3" resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-1.7.3.tgz#980f6f72d85211a5347c6b2bc18c5b84c3eb47ef" @@ -11201,6 +11222,11 @@ promise-inflight@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/promise-inflight/-/promise-inflight-1.0.1.tgz#98472870bf228132fcbdd868129bad12c3c029e3" +promise-polyfill@^7.1.1: + version "7.1.2" + resolved "https://registry.yarnpkg.com/promise-polyfill/-/promise-polyfill-7.1.2.tgz#ab05301d8c28536301622d69227632269a70ca3b" + integrity sha512-FuEc12/eKqqoRYIGBrUptCBRhobL19PS2U31vMNTfyck1FxPyMfgsXyW4Mav85y/ZN1hop3hOwRlUDok23oYfQ== + promise-retry@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/promise-retry/-/promise-retry-1.1.1.tgz#6739e968e3051da20ce6497fb2b50f6911df3d6d" @@ -13990,6 +14016,11 @@ whatwg-encoding@^1.0.1, whatwg-encoding@^1.0.3: dependencies: iconv-lite "0.4.24" +whatwg-fetch@2.0.4: + version "2.0.4" + resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-2.0.4.tgz#dde6a5df315f9d39991aa17621853d720b85566f" + integrity sha512-dcQ1GWpOD/eEQ97k66aiEVpNnapVj90/+R+SXTPYGHpYBBypfKJEQjLrvMZ7YXbKm21gXd4NcuxUTjiv1YtLng== + whatwg-fetch@>=0.10.0: version "3.0.0" resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-3.0.0.tgz#fc804e458cc460009b1a2b966bc8817d2578aefb"