Migrate integration_setting_form_spec to jest

parent 52661728
...@@ -16,7 +16,7 @@ describe('IntegrationSettingsForm', () => { ...@@ -16,7 +16,7 @@ describe('IntegrationSettingsForm', () => {
beforeEach(() => { beforeEach(() => {
integrationSettingsForm = new IntegrationSettingsForm('.js-integration-settings-form'); integrationSettingsForm = new IntegrationSettingsForm('.js-integration-settings-form');
spyOn(integrationSettingsForm, 'init'); jest.spyOn(integrationSettingsForm, 'init').mockImplementation(() => {});
}); });
it('should initialize form element refs on class object', () => { it('should initialize form element refs on class object', () => {
...@@ -130,7 +130,7 @@ describe('IntegrationSettingsForm', () => { ...@@ -130,7 +130,7 @@ describe('IntegrationSettingsForm', () => {
beforeEach(() => { beforeEach(() => {
mock = new MockAdaptor(axios); mock = new MockAdaptor(axios);
spyOn(axios, 'put').and.callThrough(); jest.spyOn(axios, 'put');
integrationSettingsForm = new IntegrationSettingsForm('.js-integration-settings-form'); integrationSettingsForm = new IntegrationSettingsForm('.js-integration-settings-form');
// eslint-disable-next-line no-jquery/no-serialize // eslint-disable-next-line no-jquery/no-serialize
...@@ -141,18 +141,13 @@ describe('IntegrationSettingsForm', () => { ...@@ -141,18 +141,13 @@ describe('IntegrationSettingsForm', () => {
mock.restore(); mock.restore();
}); });
it('should make an ajax request with provided `formData`', done => { it('should make an ajax request with provided `formData`', () => {
integrationSettingsForm return integrationSettingsForm.testSettings(formData).then(() => {
.testSettings(formData) expect(axios.put).toHaveBeenCalledWith(integrationSettingsForm.testEndPoint, formData);
.then(() => { });
expect(axios.put).toHaveBeenCalledWith(integrationSettingsForm.testEndPoint, formData);
done();
})
.catch(done.fail);
}); });
it('should show error Flash with `Save anyway` action if ajax request responds with error in test', done => { it('should show error Flash with `Save anyway` action if ajax request responds with error in test', () => {
const errorMessage = 'Test failed.'; const errorMessage = 'Test failed.';
mock.onPut(integrationSettingsForm.testEndPoint).reply(200, { mock.onPut(integrationSettingsForm.testEndPoint).reply(200, {
error: true, error: true,
...@@ -161,32 +156,27 @@ describe('IntegrationSettingsForm', () => { ...@@ -161,32 +156,27 @@ describe('IntegrationSettingsForm', () => {
test_failed: true, test_failed: true,
}); });
integrationSettingsForm return integrationSettingsForm.testSettings(formData).then(() => {
.testSettings(formData) const $flashContainer = $('.flash-container');
.then(() => {
const $flashContainer = $('.flash-container'); expect(
$flashContainer
expect( .find('.flash-text')
$flashContainer .text()
.find('.flash-text') .trim(),
.text() ).toEqual('Test failed. some error');
.trim(),
).toEqual('Test failed. some error'); expect($flashContainer.find('.flash-action')).toBeDefined();
expect(
expect($flashContainer.find('.flash-action')).toBeDefined(); $flashContainer
expect( .find('.flash-action')
$flashContainer .text()
.find('.flash-action') .trim(),
.text() ).toEqual('Save anyway');
.trim(), });
).toEqual('Save anyway');
done();
})
.catch(done.fail);
}); });
it('should not show error Flash with `Save anyway` action if ajax request responds with error in validation', done => { it('should not show error Flash with `Save anyway` action if ajax request responds with error in validation', () => {
const errorMessage = 'Validations failed.'; const errorMessage = 'Validations failed.';
mock.onPut(integrationSettingsForm.testEndPoint).reply(200, { mock.onPut(integrationSettingsForm.testEndPoint).reply(200, {
error: true, error: true,
...@@ -195,50 +185,40 @@ describe('IntegrationSettingsForm', () => { ...@@ -195,50 +185,40 @@ describe('IntegrationSettingsForm', () => {
test_failed: false, test_failed: false,
}); });
integrationSettingsForm return integrationSettingsForm.testSettings(formData).then(() => {
.testSettings(formData) const $flashContainer = $('.flash-container');
.then(() => {
const $flashContainer = $('.flash-container'); expect(
$flashContainer
expect( .find('.flash-text')
$flashContainer .text()
.find('.flash-text') .trim(),
.text() ).toEqual('Validations failed. some error');
.trim(),
).toEqual('Validations failed. some error'); expect($flashContainer.find('.flash-action')).toBeDefined();
expect(
expect($flashContainer.find('.flash-action')).toBeDefined(); $flashContainer
expect( .find('.flash-action')
$flashContainer .text()
.find('.flash-action') .trim(),
.text() ).toEqual('');
.trim(), });
).toEqual('');
done();
})
.catch(done.fail);
}); });
it('should submit form if ajax request responds without any error in test', done => { it('should submit form if ajax request responds without any error in test', () => {
spyOn(integrationSettingsForm.$form, 'submit'); jest.spyOn(integrationSettingsForm.$form, 'submit').mockImplementation(() => {});
mock.onPut(integrationSettingsForm.testEndPoint).reply(200, { mock.onPut(integrationSettingsForm.testEndPoint).reply(200, {
error: false, error: false,
}); });
integrationSettingsForm return integrationSettingsForm.testSettings(formData).then(() => {
.testSettings(formData) expect(integrationSettingsForm.$form.submit).toHaveBeenCalled();
.then(() => { });
expect(integrationSettingsForm.$form.submit).toHaveBeenCalled();
done();
})
.catch(done.fail);
}); });
it('should submit form when clicked on `Save anyway` action of error Flash', done => { it('should submit form when clicked on `Save anyway` action of error Flash', () => {
spyOn(integrationSettingsForm.$form, 'submit'); jest.spyOn(integrationSettingsForm.$form, 'submit').mockImplementation(() => {});
const errorMessage = 'Test failed.'; const errorMessage = 'Test failed.';
mock.onPut(integrationSettingsForm.testEndPoint).reply(200, { mock.onPut(integrationSettingsForm.testEndPoint).reply(200, {
...@@ -247,7 +227,7 @@ describe('IntegrationSettingsForm', () => { ...@@ -247,7 +227,7 @@ describe('IntegrationSettingsForm', () => {
test_failed: true, test_failed: true,
}); });
integrationSettingsForm return integrationSettingsForm
.testSettings(formData) .testSettings(formData)
.then(() => { .then(() => {
const $flashAction = $('.flash-container .flash-action'); const $flashAction = $('.flash-container .flash-action');
...@@ -258,44 +238,31 @@ describe('IntegrationSettingsForm', () => { ...@@ -258,44 +238,31 @@ describe('IntegrationSettingsForm', () => {
}) })
.then(() => { .then(() => {
expect(integrationSettingsForm.$form.submit).toHaveBeenCalled(); expect(integrationSettingsForm.$form.submit).toHaveBeenCalled();
});
done();
})
.catch(done.fail);
}); });
it('should show error Flash if ajax request failed', done => { it('should show error Flash if ajax request failed', () => {
const errorMessage = 'Something went wrong on our end.'; const errorMessage = 'Something went wrong on our end.';
mock.onPut(integrationSettingsForm.testEndPoint).networkError(); mock.onPut(integrationSettingsForm.testEndPoint).networkError();
integrationSettingsForm return integrationSettingsForm.testSettings(formData).then(() => {
.testSettings(formData) expect(
.then(() => { $('.flash-container .flash-text')
expect( .text()
$('.flash-container .flash-text') .trim(),
.text() ).toEqual(errorMessage);
.trim(), });
).toEqual(errorMessage);
done();
})
.catch(done.fail);
}); });
it('should always call `toggleSubmitBtnState` with `false` once request is completed', done => { it('should always call `toggleSubmitBtnState` with `false` once request is completed', () => {
mock.onPut(integrationSettingsForm.testEndPoint).networkError(); mock.onPut(integrationSettingsForm.testEndPoint).networkError();
spyOn(integrationSettingsForm, 'toggleSubmitBtnState'); jest.spyOn(integrationSettingsForm, 'toggleSubmitBtnState').mockImplementation(() => {});
integrationSettingsForm return integrationSettingsForm.testSettings(formData).then(() => {
.testSettings(formData) expect(integrationSettingsForm.toggleSubmitBtnState).toHaveBeenCalledWith(false);
.then(() => { });
expect(integrationSettingsForm.toggleSubmitBtnState).toHaveBeenCalledWith(false);
done();
})
.catch(done.fail);
}); });
}); });
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment