From 076ab379bf99719dd6d11fb5d1e78bee127bddbe Mon Sep 17 00:00:00 2001 From: Shakya Peiris Date: Thu, 24 Nov 2022 09:39:21 +0000 Subject: [PATCH] Write tests for Search Component --- src/__tests__/home/Search.test.ts | 48 ++++++++++++++++ .../home/__snapshots__/Search.test.ts.snap | 56 +++++++++++++++++++ src/lib/home/Search.svelte | 3 + 3 files changed, 107 insertions(+) create mode 100644 src/__tests__/home/Search.test.ts create mode 100644 src/__tests__/home/__snapshots__/Search.test.ts.snap diff --git a/src/__tests__/home/Search.test.ts b/src/__tests__/home/Search.test.ts new file mode 100644 index 0000000..fc7e4fb --- /dev/null +++ b/src/__tests__/home/Search.test.ts @@ -0,0 +1,48 @@ +import { describe, expect, test, afterEach } from 'vitest'; +import { render, screen, cleanup, fireEvent } from '@testing-library/svelte'; +import SearchBar from '../../lib/home/Search.svelte'; +import { districts } from '../../data'; +describe('Unit:Home/Search', () => { + let searchValue = ''; + let selectValue = ''; + afterEach(() => { + cleanup(); + }); + + test('should change value according to change event', async () => { + render(SearchBar, { searchValue, selectValue }); + + const inputText = 'Test search'; + const inputElements = screen.getAllByTestId('search-input'); + + inputElements.forEach(async (inputElement) => { + await fireEvent.input(inputElement, { + target: { + value: inputText, + }, + }); + + expect((inputElement as HTMLInputElement).value).toBe(inputText); + }); + }); + + test('should change select value upon change', () => { + render(SearchBar, { searchValue, selectValue }); + + const selectElement = screen.getByTestId('search-select'); + districts.forEach((district) => { + fireEvent.select(selectElement, { + target: { + value: district, + }, + }); + expect((selectElement as HTMLSelectElement).value).toBe(district); + }); + }); + + test('snapshot test', () => { + render(SearchBar, { searchValue, selectValue }); + + expect(screen).toMatchSnapshot(); + }); +}); diff --git a/src/__tests__/home/__snapshots__/Search.test.ts.snap b/src/__tests__/home/__snapshots__/Search.test.ts.snap new file mode 100644 index 0000000..5ab4697 --- /dev/null +++ b/src/__tests__/home/__snapshots__/Search.test.ts.snap @@ -0,0 +1,56 @@ +// Vitest Snapshot v1 + +exports[`Unit:Home/Search > snapshot test 1`] = ` +{ + "debug": [Function], + "findAllByAltText": [Function], + "findAllByDisplayValue": [Function], + "findAllByLabelText": [Function], + "findAllByPlaceholderText": [Function], + "findAllByRole": [Function], + "findAllByTestId": [Function], + "findAllByText": [Function], + "findAllByTitle": [Function], + "findByAltText": [Function], + "findByDisplayValue": [Function], + "findByLabelText": [Function], + "findByPlaceholderText": [Function], + "findByRole": [Function], + "findByTestId": [Function], + "findByText": [Function], + "findByTitle": [Function], + "getAllByAltText": [Function], + "getAllByDisplayValue": [Function], + "getAllByLabelText": [Function], + "getAllByPlaceholderText": [Function], + "getAllByRole": [Function], + "getAllByTestId": [Function], + "getAllByText": [Function], + "getAllByTitle": [Function], + "getByAltText": [Function], + "getByDisplayValue": [Function], + "getByLabelText": [Function], + "getByPlaceholderText": [Function], + "getByRole": [Function], + "getByTestId": [Function], + "getByText": [Function], + "getByTitle": [Function], + "logTestingPlaygroundURL": [Function], + "queryAllByAltText": [Function], + "queryAllByDisplayValue": [Function], + "queryAllByLabelText": [Function], + "queryAllByPlaceholderText": [Function], + "queryAllByRole": [Function], + "queryAllByTestId": [Function], + "queryAllByText": [Function], + "queryAllByTitle": [Function], + "queryByAltText": [Function], + "queryByDisplayValue": [Function], + "queryByLabelText": [Function], + "queryByPlaceholderText": [Function], + "queryByRole": [Function], + "queryByTestId": [Function], + "queryByText": [Function], + "queryByTitle": [Function], +} +`; diff --git a/src/lib/home/Search.svelte b/src/lib/home/Search.svelte index cf676ba..682a9de 100644 --- a/src/lib/home/Search.svelte +++ b/src/lib/home/Search.svelte @@ -13,6 +13,7 @@ class="w-[65%] px-[25px] py-[10px] bg-[#E7E7E7] text-[#6C6C6C] rounded-full" placeholder="Enter the checmical name" bind:value={searchValue} + data-testid="search-input" />