Skip to content
This repository has been archived by the owner on Aug 25, 2020. It is now read-only.

Commit

Permalink
Testing Interceptors in more depth.
Browse files Browse the repository at this point in the history
+ fixing some errors in the submit "chain"
+ updating dependencies for the project.
  • Loading branch information
Nevoss committed Dec 15, 2018
1 parent 4e560e3 commit b700391
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 26 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ module.exports = {
preset: 'ts-jest',
collectCoverage: true,
coverageDirectory: './coverage',
setupTestFrameworkScriptFile: 'jest-extended',
}
67 changes: 54 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@
"author": "Nevo Golan <nevos12@gmail.com>",
"license": "MIT",
"devDependencies": {
"@types/jest": "^23.3.9",
"@types/jest": "^23.3.10",
"jest": "^23.6.0",
"jest-extended": "^0.11.0",
"prettier": "1.15.3",
"rollup": "^0.67.4",
"rollup-plugin-typescript2": "^0.18.0",
"ts-jest": "^23.10.4",
"typescript": "^3.1.6"
"rollup-plugin-typescript2": "^0.18.1",
"ts-jest": "^23.10.5",
"typescript": "^3.2.2"
}
}
10 changes: 4 additions & 6 deletions src/core/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ export class Form {
*/
public static defaultOptions: Options = defaultsOptions

/**
* Interceptors that will run in every submission
*/
public static defaultInterceptors: InterceptorManagersObject

/**
* determine if the form is on submitting mode
*/
Expand Down Expand Up @@ -319,7 +314,10 @@ export class Form {
* @param callback
*/
public submit(callback: SubmitCallback): Promise<any> {
let chain: any[] = [this.wrapSubmitCallBack(callback), null]
let chain: any[] = [
this.wrapSubmitCallBack(callback),
error => Promise.reject({ error, form: this }),
]

this.$interceptors.beforeSubmission
.merge(basicInterceptors.beforeSubmission)
Expand Down
5 changes: 4 additions & 1 deletion src/core/InterceptorManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export class InterceptorManager {
* @param fulfilled
* @param rejected
*/
public use(fulfilled: Function | null, rejected: Function | null): number {
public use(
fulfilled: Function | null,
rejected: Function | null = null
): number {
this.$handlers.push({
fulfilled,
rejected,
Expand Down
2 changes: 1 addition & 1 deletion src/interceptors/beforeSubmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { InterceptorHandler } from '../types'
export const validateForm: InterceptorHandler = {
fulfilled: (form: Form): Promise<any> => {
if (form.$options.validation.onSubmission && !form.validate()) {
return Promise.reject({ error: { message: 'Form is invalid.' }, form })
return Promise.reject({ message: 'Form is invalid.' })
}

return Promise.resolve(form)
Expand Down
100 changes: 100 additions & 0 deletions tests/core/Form/Form.interceptors.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { Form } from '../../../src/core/Form'

jest.mock('../../../src/core/Errors')
jest.mock('../../../src/core/Validator')
jest.mock('../../../src/core/Touched')

describe('Form.interceptors.ts', () => {
it('should add new interceptor to the end of the chain when using submissionComplete interceptorManager', async () => {
let form = new Form({})

const fulfilledFunc = jest.fn()
const rejectedFunc = jest.fn()

form.$interceptors.submissionComplete.use(fulfilledFunc, rejectedFunc)

let callback = jest.fn(() => Promise.resolve('yay!'))

await form.submit(callback)

expect(fulfilledFunc).toHaveBeenCalledTimes(1)
expect(fulfilledFunc).toHaveBeenLastCalledWith({ response: 'yay!', form })
expect(fulfilledFunc).toHaveBeenCalledAfter(callback)
expect(rejectedFunc).toHaveBeenCalledTimes(0)

fulfilledFunc.mockClear()
rejectedFunc.mockClear()
callback.mockClear()

callback = jest.fn(() => Promise.reject('Oh...'))

await form.submit(callback)

expect(rejectedFunc).toHaveBeenCalledTimes(1)
expect(rejectedFunc).toHaveBeenLastCalledWith({ error: 'Oh...', form })
expect(rejectedFunc).toHaveBeenCalledAfter(callback)
expect(fulfilledFunc).toHaveBeenCalledTimes(0)
})

it('should add new interceptor to the begging of the chain when using beforeSubmission interceptorManager', async () => {
let form = new Form({})

expect.assertions(8)

const fulfilledFunc = jest.fn(form => form)
const rejectedFunc = jest.fn(error => Promise.reject(error))

form.$interceptors.beforeSubmission.use(fulfilledFunc, rejectedFunc)

let callback = jest.fn(() => Promise.resolve('yay!'))

await form.submit(callback)

expect(fulfilledFunc).toHaveBeenCalledTimes(1)
expect(fulfilledFunc).toHaveBeenLastCalledWith(form)
expect(fulfilledFunc).toHaveBeenCalledBefore(callback)
expect(rejectedFunc).toHaveBeenCalledTimes(0)

fulfilledFunc.mockClear()
rejectedFunc.mockClear()
callback.mockClear()

form.$interceptors.beforeSubmission.use(() => Promise.reject('Error!'))

try {
await form.submit(callback)
} catch (e) {
expect(rejectedFunc).toHaveBeenCalledTimes(1)
expect(rejectedFunc).toHaveBeenLastCalledWith('Error!')
expect(callback).toHaveBeenCalledTimes(0)
expect(fulfilledFunc).toHaveBeenCalledTimes(0)
}
})

it('should not call the interceptor if the user eject it', async () => {
let form = new Form({})

expect.assertions(4)

const fulfilledFunc = jest.fn()
const rejectedFunc = jest.fn()

const interceptorIndex = form.$interceptors.submissionComplete.use(
fulfilledFunc,
rejectedFunc
)
form.$interceptors.submissionComplete.eject(interceptorIndex)

await form.submit(() => Promise.resolve())

expect(fulfilledFunc).toHaveBeenCalledTimes(0)
expect(rejectedFunc).toHaveBeenCalledTimes(0)

try {
await form.submit(() => Promise.reject())
} catch (e) {
expect(fulfilledFunc).toHaveBeenCalledTimes(0)
expect(rejectedFunc).toHaveBeenCalledTimes(0)
}
})
})
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
"module": "es2015",
"target": "es2015",
"declaration": true,
"esModuleInterop": true
"esModuleInterop": true,
"types": [
"jest",
"jest-extended"
]
},
"include": [
"src/**/*"
Expand Down

0 comments on commit b700391

Please sign in to comment.