import 'reflect-metadata'; import { FormPostService, } from './form-post.service'; const initFormPostService = ( window?: Window, ) => { return new FormPostService(window); }; describe('formPost', () => { const initMockCreatedElement = () => { const mockCreatedElement = {} as HTMLFormElement; mockCreatedElement.setAttribute = jest.fn(); mockCreatedElement.appendChild = jest.fn(); mockCreatedElement.submit = jest.fn(); return mockCreatedElement; }; const initMockWindow = () => { const window = { document: { body: {}, }, } as Window; window.document.body.appendChild = jest.fn(); return window; }; const initPostData = () => { const mockCreatedFormElement = initMockCreatedElement(); const mockCreatedInputElement = initMockCreatedElement(); const window = initMockWindow(); window.document.createElement = jest.fn() .mockReturnValueOnce(mockCreatedFormElement) .mockReturnValueOnce(mockCreatedInputElement); const kitePrintSdk = initFormPostService(window); return { kitePrintSdk, mockCreatedFormElement, mockCreatedInputElement, window, }; }; test('Creates a new form on the document', () => { const { kitePrintSdk, window, } = initPostData(); kitePrintSdk.formPost('test', 'data'); expect(window.document.createElement).toHaveBeenCalledWith('form'); }); test('Sets the form attribute to post', () => { const { mockCreatedFormElement, kitePrintSdk, } = initPostData(); kitePrintSdk.formPost('test', 'data'); expect( mockCreatedFormElement.setAttribute, ).toHaveBeenCalledWith('method', 'post'); }); test('Sets the form action to the path provided', () => { const { mockCreatedFormElement, kitePrintSdk, } = initPostData(); kitePrintSdk.formPost('testpath', 'data'); expect( mockCreatedFormElement.setAttribute, ).toHaveBeenCalledWith('action', 'testpath'); }); test('Sets the form acceptCharset to utf-8', () => { const { mockCreatedFormElement, kitePrintSdk, } = initPostData(); kitePrintSdk.formPost('testpath', 'data'); expect(mockCreatedFormElement.acceptCharset).toBe('utf-8'); }); test('Creates a new input on the document', () => { const { kitePrintSdk, window, } = initPostData(); kitePrintSdk.formPost('test', 'data'); expect(window.document.createElement).toHaveBeenCalledWith('input'); }); const testSetsInputAttribute = ({ expectedAttributeName, expectedAttributeValue, jsonData = 'datatest', }: { expectedAttributeName: string, expectedAttributeValue: string, jsonData?: string, }) => { const { kitePrintSdk, mockCreatedInputElement, } = initPostData(); kitePrintSdk.formPost('test', jsonData); expect(mockCreatedInputElement.setAttribute).toHaveBeenCalledWith( expectedAttributeName, expectedAttributeValue, ); }; test('Sets the input to hidden', () => { testSetsInputAttribute({ expectedAttributeName: 'type', expectedAttributeValue: 'hidden', }); }); test('Sets the input name to data', () => { testSetsInputAttribute({ expectedAttributeName: 'name', expectedAttributeValue: 'data', }); }); test('Sets the input value to the json data', () => { testSetsInputAttribute({ expectedAttributeName: 'value', expectedAttributeValue: 'testdata123', jsonData: 'testdata123', }); }); test('Appends the input to the form', () => { const { kitePrintSdk, mockCreatedFormElement, mockCreatedInputElement, } = initPostData(); kitePrintSdk.formPost('test', 'data'); expect( mockCreatedFormElement.appendChild, ).toHaveBeenCalledWith(mockCreatedInputElement); }); test('Appends the form to the document body', () => { const { kitePrintSdk, mockCreatedFormElement, window, } = initPostData(); kitePrintSdk.formPost('test', 'data'); expect( window.document.body.appendChild, ).toHaveBeenCalledWith(mockCreatedFormElement); }); test('Calls form submit', () => { const { kitePrintSdk, mockCreatedFormElement, } = initPostData(); kitePrintSdk.formPost('test', 'data'); expect(mockCreatedFormElement.submit).toHaveBeenCalled(); }); });