Browse Source

update search input test

tumao 4 years ago
parent
commit
f93baef9e9
1 changed files with 20 additions and 0 deletions
  1. 20 0
      client/src/components/__test__/customInput/SearchInput.spec.tsx

+ 20 - 0
client/src/components/__test__/customInput/SearchInput.spec.tsx

@@ -5,11 +5,31 @@ import i18n from '../../../i18n';
 
 describe('test search input component', () => {
   it('renders default state', () => {
+    const mockSearchFn = jest.fn();
+    const container = render(
+      <I18nextProvider i18n={i18n}>
+        <SearchInput searchText="search text" onSearch={mockSearchFn} />
+      </I18nextProvider>
+    );
+
+    // material textfield input role is textbox
+    expect(container.getByRole('textbox')).toBeInTheDocument();
+    expect(container.getByRole('textbox')).toHaveValue('search text');
+  });
+
+  it('checks input value change event', () => {
     const mockSearchFn = jest.fn();
     const container = render(
       <I18nextProvider i18n={i18n}>
         <SearchInput onSearch={mockSearchFn} />
       </I18nextProvider>
     );
+
+    const input = container.getByRole('textbox');
+    fireEvent.change(input, { target: { value: 'search change test' } });
+    expect(input).toHaveValue('search change test');
+    // mock Enter key press event
+    fireEvent.keyPress(input, { key: 'Enter', code: 13, charCode: 13 });
+    expect(mockSearchFn).toBeCalledTimes(1);
   });
 });