Skip to content

Commit

Permalink
feat(core): INT-1997 Add optional target parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricio-sg committed Oct 23, 2019
1 parent 61dea44 commit 6676d75
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/form-builder.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export default class FormBuilder {
build(url: string, data: { [key: string]: any }): HTMLFormElement {
build(url: string, data: { [key: string]: any }, target?: string): HTMLFormElement {
const form = document.createElement('form');

form.style.display = 'none';

form.setAttribute('action', url);
form.setAttribute('method', 'POST');
form.setAttribute('target', '_top');
form.setAttribute('target', target || '_top');

Object.keys(data)
.forEach(key => {
Expand Down
4 changes: 2 additions & 2 deletions src/form-poster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export default class FormPoster {
private _options?: FormPosterOptions
) {}

postForm(url: string, data: { [key: string]: any }, callback?: () => void): void {
const form = this._formBuilder.build(this._prependHost(url), data);
postForm(url: string, data: { [key: string]: any }, callback?: () => void, target?: string): void {
const form = this._formBuilder.build(this._prependHost(url), data, target);

window.addEventListener('unload', function handleUnload() {
window.removeEventListener('unload', handleUnload);
Expand Down
16 changes: 15 additions & 1 deletion test/form-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ describe('FormBuilder', () => {
describe('#build()', () => {
const url = '/url/123';
const data = { field_1: 'foo', field_2: 'bar' };
const target = 'target_iframe';

it('returns a HTML form with hidden input fields', () => {
it('returns a HTML form with hidden input fields with the default target if no target is provided', () => {
const output = formBuilder.build(url, data);
const expectedOutput = (
'<form style="display: none;" action="/url/123" method="POST" target="_top">' +
Expand All @@ -23,5 +24,18 @@ describe('FormBuilder', () => {
expect(output.outerHTML)
.toEqual(expectedOutput);
});

it('returns a HTML form with hidden input fields and overrides the target when provided', () => {
const output = formBuilder.build(url, data, target);
const expectedOutput = (
'<form style="display: none;" action="/url/123" method="POST" target="' + target + '">' +
'<input name="field_1" type="hidden" value="foo">' +
'<input name="field_2" type="hidden" value="bar">' +
'</form>'
);

expect(output.outerHTML)
.toEqual(expectedOutput);
});
});
});
17 changes: 14 additions & 3 deletions test/form-poster.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,28 @@ describe('FormPoster', () => {
describe('#postForm()', () => {
const url = '/url/123';
const data = { field_1: 'foo', field_2: 'bar' };
const target = 'target_iframe';

beforeEach(() => {
jest.spyOn(form, 'submit')
.mockImplementation(jest.fn());
});

it('posts the data using a hidden HTML form', () => {
it('posts the data using a hidden HTML form with the default target', () => {
formPoster.postForm(url, data);

expect(formBuilder.build)
.toHaveBeenCalledWith(url, data);
.toHaveBeenCalledWith(url, data, undefined);

expect(form.submit)
.toHaveBeenCalled();
});

it('posts the data using a hidden HTML form with the provided target', () => {
formPoster.postForm(url, data, undefined, target);

expect(formBuilder.build)
.toHaveBeenCalledWith(url, data, target);

expect(form.submit)
.toHaveBeenCalled();
Expand All @@ -41,7 +52,7 @@ describe('FormPoster', () => {
formPoster.postForm(url, data);

expect(formBuilder.build)
.toHaveBeenCalledWith('https://foobar.com/url/123', data);
.toHaveBeenCalledWith('https://foobar.com/url/123', data, undefined);
});

it('triggers the callback after posting the data', () => {
Expand Down

0 comments on commit 6676d75

Please sign in to comment.