|
import { SubmissionError } from 'redux-form'; |
|
import API from '../../../API'; |
|
import { addToast } from '../toasts'; |
|
import { submitForm } from './formsActions'; |
|
|
|
jest.mock('redux-form'); |
|
jest.mock('../../../API'); |
|
jest.mock('../toasts'); |
|
|
|
describe('form actions', () => { |
|
// reset mocks before each test |
|
beforeEach(() => jest.resetAllMocks()); |
|
|
|
test('SubmitForm must include an object item/values', () => { |
|
expect(() => submitForm()).toThrow(Error); |
|
expect(() => submitForm({ url: 'http://example.com' })).toThrow(Error); |
|
expect(() => submitForm({ item: 'Resource' })).toThrow(Error); |
|
expect(() => submitForm({ values: { a: 1 } })).toThrow(Error); |
|
}); |
|
|
|
test('should submit form correctly', async () => { |
|
API.post.mockImplementation(async () => ({ data: 'some-data' })); |
|
addToast.mockImplementation(() => 'some-toast'); |
|
|
|
const dispatch = jest.fn(); |
|
|
|
const dispatcher = submitForm({ |
|
values: 'some-values', |
|
url: 'some-url', |
|
item: 'some-item', |
|
}); |
|
|
|
await dispatcher(dispatch); |
|
|
|
expect(API.post).toMatchSnapshot(); |
|
expect(addToast).toMatchSnapshot(); |
|
expect(dispatch).toMatchSnapshot(); |
|
}); |
|
|
|
test('should fail correctly when submiting form', async () => { |
|
const dispatch = jest.fn(); |
|
|
|
API.post.mockImplementation(async () => { |
|
// eslint-disable-next-line no-throw-literal |
|
throw { |
|
response: { |
|
status: 'some-status', |
|
statusText: 'some-status-text', |
|
data: 'some-data', |
|
}, |
|
}; |
|
}); |
|
|
|
const dispatcher = submitForm({ |
|
values: 'some-values', |
|
url: 'some-url', |
|
item: 'some-item', |
|
}); |
|
|
|
await expect(dispatcher(dispatch)).rejects.toThrow(SubmissionError); |
|
|
|
expect(SubmissionError).toMatchSnapshot(); |
|
expect(API.post).toMatchSnapshot(); |
|
expect(addToast).toMatchSnapshot(); |
|
expect(dispatch).toMatchSnapshot(); |
|
}); |
|
|
|
test('should fail correctly with status=422 when submiting form', async () => { |
|
const dispatch = jest.fn(); |
|
|
|
API.post.mockImplementation(async () => { |
|
// eslint-disable-next-line no-throw-literal |
|
throw { |
|
response: { |
|
status: 422, |
|
statusText: 'some-status-text', |
|
data: { error: { errors: { base: 'some-error', name: 'some-name' } } }, |
|
}, |
|
}; |
|
}); |
|
|
|
const dispatcher = submitForm({ |
|
values: 'some-values', |
|
url: 'some-url', |
|
item: 'some-item', |
|
}); |
|
|
|
await expect(dispatcher(dispatch)).rejects.toThrow(SubmissionError); |
|
|
|
expect(SubmissionError).toMatchSnapshot(); |
|
expect(API.post).toMatchSnapshot(); |
|
expect(addToast).toMatchSnapshot(); |
|
expect(dispatch).toMatchSnapshot(); |
|
}); |
|
}); |