Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
47 changes: 47 additions & 0 deletions core/modules/mailer/test/unit/sendEmail.spec.ts
Original file line number Diff line number Diff line change
@@ -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}`)
}
})
})
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion test/unit/jest.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ module.exports = {
'<rootDir>/node_modules/(?!lodash-es/.*)'
],
setupFiles: [
'<rootDir>/test/unit/setupTestEnvironment.js'
'<rootDir>/test/unit/setupTestEnvironment.ts'
]
}
1 change: 0 additions & 1 deletion test/unit/setupTestEnvironment.js

This file was deleted.

6 changes: 6 additions & 0 deletions test/unit/setupTestEnvironment.ts
Original file line number Diff line number Diff line change
@@ -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;
31 changes: 31 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down