Thuta Learning
React
AdvancedWeb Developmentအလယ်အလတ်

Component Testing & Accessibility

Reactသင်ခန်းစာ 2535 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Implementation detail အစား user မြင်တဲ့ behaviour ကို စစ်တဲ့ test ရေးနိုင်ဖို့
  • getByRole၊ getByLabelText စတဲ့ query တွေကို ဦးစားပေး အစီအစဉ်အတိုင်း ရွေးနိုင်ဖို့
  • getBy၊ queryBy၊ findBy ကွာခြားချက်ကို သိပြီး မရှိခြင်းနဲ့ async UI ကို စစ်နိုင်ဖို့
  • Vitest မှာ jsdom၊ jest-dom matcher နဲ့ cleanup ကို setup လုပ်နိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

Component test ဆိုတာ robot user တစ်ယောက်ကို app ကို သုံးခိုင်းပြီး ရလဒ်ကို စစ်တာပါ။ Robot က "Add" ဆိုတဲ့ button ကို ရှာပြီး နှိပ်တယ်၊ ပြီးရင် screen ပေါ်မှာ "Count: 1" ပေါ်လားလို့ ကြည့်တယ်။ Code ထဲက state variable နာမည် ဒါမှမဟုတ် CSS class နာမည်ကို မကြည့်ပါဘူး။

စိတ်ဝင်စားစရာက robot ဟာ screen reader အသုံးပြုသူလို element တွေကို role နဲ့ နာမည်နဲ့ ရှာတာပါ။ Robot ရှာမတွေ့တဲ့ button ကို screen reader သုံးတဲ့ လူတွေလည်း ရှာတွေ့ဖို့ ခက်ပါတယ်။ ဒါကြောင့် test ရေးရင်း accessibility ကိုပါ တစ်ခါတည်း စစ်ပြီးသား ဖြစ်ပါတယ်။

Problem → Why → Solution

ပြဿနာ: pass ဖြစ်ပေမယ့် ဘာမှ မကာကွယ်တဲ့ test

အောက်က SaveBar မှာ Save "button" က တကယ်တော့ onClick ပါတဲ့ div တစ်ခုပါ။ Test က CSS class နဲ့ element ကို ရှာပြီး click လုပ်လို့ pass ဖြစ်ပါတယ်။ ဒါပေမယ့် keyboard user က Tab နဲ့ ဒီ div ဆီ မရောက်နိုင်သလို screen reader ကလည်း button လို့ မကြေညာပါဘူး။ Test က ဒီ bug ကို လုံးဝ မမြင်ပါဘူး။

နောက်တစ်ခုက designer က btn-primary ကို btn-main လို့ နာမည်ပြောင်းလိုက်ရင် behaviour ဘာမှ မပြောင်းပေမယ့် test က fail ဖြစ်ပါတယ်။ အမှန်တကယ် ကျိုးတဲ့အခါ မသတိပေးဘဲ မကျိုးတဲ့အခါမှ အော်တဲ့ test မျိုးပါ။

SaveBar.test.jsx
// SaveBar.jsx renders: <div className="btn-primary" onClick={save}>Save</div>

test('saves', () => {
  const { container } = render(<SaveBar />);
  fireEvent.click(container.querySelector('.btn-primary'));   // finds by CSS class
  expect(container.querySelector('.toast').textContent).toBe('Saved');
});

ဘာကြောင့် ဒီ test က အားနည်းတာလဲ

Test က user မသိတဲ့ အရာ (class နာမည်၊ DOM structure) ကို မှီခိုနေလို့ refactor တိုင်းမှာ ကျိုးပြီး user သိတဲ့ အရာ (role၊ label၊ စာသား) ကိုတော့ မစစ်ပါဘူး။ getByRole('button', { name: 'Save' }) နဲ့ ရှာမယ်ဆိုရင် div ကို ရှာမတွေ့လို့ test က ချက်ချင်း fail ဖြစ်ပြီး accessibility bug ကို ဖော်ပြပေးမှာပါ။

Core concept: user လို query လုပ်ပြီး user လို လုပ်ဆောင်ပါ

Testing Library ရဲ့ အဓိက အယူအဆက "Test တွေက software ကို သုံးတဲ့ပုံနဲ့ တူလေ၊ ယုံကြည်မှု ပိုပေးလေ" ဆိုတာပါ။ Vitest က test တွေကို run ပြီး jsdom က Node ထဲမှာ browser DOM အတုကို ပေးပါတယ်။ React Testing Library ရဲ့ render() က component ကို အဲဒီ DOM ထဲ mount လုပ်ပြီး screen object က document တစ်ခုလုံးကို ရှာဖို့ query တွေ ပေးပါတယ်။

၁။ Query ကို ဦးစားပေး အစီအစဉ်နဲ့ ရွေးပါ

getByRole ကို ပထမ ရွေးပါ။ ပြီးရင် form field တွေအတွက် getByLabelText၊ နောက် getByPlaceholderText၊ getByText၊ getByDisplayValue၊ getByAltText၊ getByTitle ဆက်လာပြီး getByTestId က နောက်ဆုံး ရွေးချယ်စရာပါ။

၂။ userEvent နဲ့ လူလို လုပ်ဆောင်ပါ

userEvent.setup() ပြီးရင် await user.click()၊ await user.type() ကို သုံးပါ။ ဒါတွေက focus၊ keydown၊ input စတဲ့ event အစဉ်လိုက်ကို အစစ်လို ဖန်တီးပေးလို့ fireEvent.click() တစ်ခုတည်းထက် ပိုလက်တွေ့ကျပါတယ်။

၃။ Query အမျိုးအစားကို ရည်ရွယ်ချက်နဲ့ ကိုက်ပါ

getBy က element မတွေ့ရင် throw လုပ်ပါတယ်။ queryBy က null ပြန်ပေးလို့ "မရှိဘူး" ဆိုတာကို စစ်ဖို့ သုံးပါတယ်။ findBy က promise ပြန်ပေးပြီး element ပေါ်လာတဲ့အထိ ခဏ စောင့်ပေးလို့ fetch ပြီးမှ ပေါ်တဲ့ UI အတွက် သုံးပါတယ်။

getByRole က button ရဲ့ နာမည်ကို ဘယ်လို သိတာလဲ။ Browser တွေ တွက်တဲ့ နည်းအတိုင်း accessible name ကို တွက်ပါတယ်: button ထဲက စာသား၊ aria-label၊ htmlFor နဲ့ id ကနေ input နဲ့ ချိတ်ထားတဲ့ <label>၊ ပုံအတွက် alt စာသား စတာတွေကနေ ယူပါတယ်။ ဒါကြောင့် getByRole('textbox', { name: 'Email' }) မှာ fail ဖြစ်ရင် test ပျက်နေတာထက် label ပျောက်နေတာကို ညွှန်ပြလေ့ရှိပါတယ်။ မျက်စိနဲ့ မြင်ရတဲ့ label ကို ဦးစားပေးပြီး aria-label ကိုတော့ မြင်ရတဲ့ စာသား မထည့်နိုင်တဲ့ icon button လို နေရာတွေမှာပဲ သုံးပါ။

Queryမတွေ့ရင်သုံးရမယ့်အချိန်
getByRole(...)Error throwအခုချက်ချင်း ရှိနေရမယ့် element
queryByRole(...)null ပြန်ပေးမရှိကြောင်း စစ်ဖို့
await findByRole(...)Timeout ပြီးမှ rejectနောက်မှ ပေါ်လာမယ့် element
Mental model: robot screen-reader user
  1. Component ကို jsdom ထဲ render
  2. Role နဲ့ နာမည်နဲ့ element ကို ရှာ
  3. userEvent နဲ့ နှိပ်/ရိုက်
  4. Screen ပေါ်က ရလဒ်ကို assert

Robot ရှာမတွေ့ရင် test ကို ပြင်ဖို့ မဟုတ်ဘဲ component ကို accessible ဖြစ်အောင် ပြင်ဖို့ အချက်ပြတာ ဖြစ်တတ်ပါတယ်။

အခြေခံ ပုံစံ

jsx
test('describes what the user sees', async () => {
  const user = userEvent.setup();
  render(<Component {...props} />);

  await user.type(screen.getByLabelText('Field label'), 'some text');
  await user.click(screen.getByRole('button', { name: /action/i }));

  expect(await screen.findByText('Result')).toBeInTheDocument();
  expect(screen.queryByRole('alert')).not.toBeInTheDocument();
});

Arrange (render)၊ Act (user လုပ်ဆောင်ချက်)၊ Assert (screen ပေါ်က ရလဒ်) ဆိုတဲ့ အဆင့်သုံးဆင့်ပါ။ Element တွေကို label၊ role နဲ့ နာမည်နဲ့ ရှာပြီး async ရလဒ်ကို findBy နဲ့ စောင့်ပါ။

Counter ကို accessible button နဲ့ test လုပ်ခြင်း

Test လုပ်မယ့် Counter component က ဒီလိုပါ။ Button မှာ "Add" ဆိုတဲ့ စာသားပါလို့ အဲဒါက button ရဲ့ accessible name ဖြစ်ပါတယ်။ Test file က Counter.jsx ဘေးမှာ Counter.test.jsx အဖြစ် ရှိပြီး npx vitest နဲ့ run ပါတယ်။

Counter.jsx
import { useState } from 'react';

export default function Counter() {
  const [count, setCount] = useState(0);
  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Add</button>
    </div>
  );
}
jsx
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { expect, test } from 'vitest';
import Counter from './Counter.jsx';

test('increments from an accessible button', async () => {
  const user = userEvent.setup();
  render(<Counter />);
  await user.click(screen.getByRole('button', { name: /add/i }));
  expect(screen.getByText('Count: 1')).toBeInTheDocument();
});

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. A simulated userconst user = userEvent.setup();

    Test တစ်ခုစီရဲ့ အစမှာ user session တစ်ခု ဖန်တီးပါတယ်။ ဒီ object ရဲ့ method တွေက promise ပြန်လို့ await လုပ်ရပါတယ်။

  2. Mount into jsdomrender(<Counter />);

    Counter ကို jsdom ရဲ့ document.body ထဲ render လုပ်ပါတယ်။ ပြီးရင် screen query တွေက အဲဒီ document ကို ရှာပါတယ်။

  3. Find by role and nameawait user.click(screen.getByRole('button', { name: /add/i }));

    "add" ဆိုတဲ့ accessible name ပါတဲ့ button ကို case မခွဲဘဲ ရှာပြီး နှိပ်ပါတယ်။ Button ကို div ပြောင်းလိုက်ရင် ဒီ line က fail ဖြစ်ပါမယ်။

  4. Assert on visible textexpect(screen.getByText('Count: 1')).toBeInTheDocument();

    State variable ကို မစစ်ဘဲ user မြင်ရတဲ့ စာသားကို စစ်ပါတယ်။ toBeInTheDocument က jest-dom ကလာတဲ့ matcher ဖြစ်လို့ setup file ထဲမှာ import လုပ်ထားရပါမယ်။

You should see
Vitest က test တစ်ခု pass ဖြစ်ကြောင်း ပြပါတယ် (ဥပမာ "✓ increments from an accessible button" နဲ့ "Tests 1 passed (1)" လို summary)။ jsdom environment နဲ့ jest-dom matcher ကို setup လုပ်ထားမှသာ toBeInTheDocument() အလုပ်လုပ်ပါတယ်။

Vitest setup: jsdom၊ jest-dom နဲ့ cleanup

Vitest က default အနေနဲ့ Node environment မှာ run လို့ document မရှိပါဘူး။ Config ထဲမှာ environment: 'jsdom' ထည့်ပြီး jsdom package ကို install လုပ်ရပါတယ်။ Setup file က test တိုင်းမတိုင်ခင် run ပြီး jest-dom matcher တွေကို ထည့်ပေးပါတယ်။ Vitest ရဲ့ globals option ကို မဖွင့်ထားရင် Testing Library က test တစ်ခုပြီးတိုင်း DOM ကို အလိုလို မရှင်းနိုင်လို့ afterEach ထဲမှာ cleanup() ကို ကိုယ်တိုင် ခေါ်ထားတာ လုံခြုံပါတယ်။

vitest.config.js + src/setupTests.js
// vitest.config.js
import { defineConfig } from 'vitest/config';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
  test: { environment: 'jsdom', setupFiles: './src/setupTests.js' },
});

// src/setupTests.js
import '@testing-library/jest-dom/vitest';
import { cleanup } from '@testing-library/react';
import { afterEach } from 'vitest';

afterEach(() => cleanup());

လက်တွေ့ project တွေမှာ

လက်တွေ့ project တွေမှာ

လက်တွေ့ team တွေမှာ component test က pull request တိုင်းမှာ CI ထဲ run ပါတယ်။ Login form၊ checkout step၊ search filter လို user flow အရေးကြီးတဲ့ နေရာတွေမှာ test ကို ဦးစားပေးရေးပြီး button အရောင်လို visual detail တွေကိုတော့ test မလုပ်ပါဘူး။ API ကို ခေါ်တဲ့ component တွေအတွက် MSW (Mock Service Worker) လို tool နဲ့ network ကို mock လုပ်ပြီး loading၊ error၊ success state သုံးမျိုးလုံးကို test လုပ်လေ့ရှိပါတယ်။

Accessibility ဘက်မှာတော့ eslint-plugin-jsx-a11y က label မပါတဲ့ input လို ပြဿနာတွေကို code ရေးနေတုန်း သတိပေးပြီး axe-based tool တွေက render ပြီးသား DOM ကို automated စစ်ဆေးပါတယ်။ ဒါပေမယ့် automated tool တွေက ပြဿနာ တစ်စိတ်တစ်ပိုင်းကိုသာ ဖမ်းနိုင်လို့ keyboard တစ်ခုတည်းနဲ့ app ကို ကိုယ်တိုင် သုံးကြည့်တာကို အစားထိုးလို့ မရပါဘူး။

အမှားများ

ဖြစ်လေ့ရှိတဲ့ အမှားများ

"မရှိဘူး" ကို getBy နဲ့ စစ်တာ

expect(screen.getByRole('alert')).not.toBeInTheDocument() က alert မရှိရင် expect ရောက်မလာခင် getBy ကပဲ throw လုပ်လို့ test fail ဖြစ်ပါတယ်။ မရှိခြင်းကို queryByRole နဲ့ စစ်ပါ။

userEvent ကို await မလုပ်တာ

user.click(...) ကို await မပါဘဲ ခေါ်ရင် click မပြီးခင် assertion run သွားပြီး တစ်ခါ pass တစ်ခါ fail ဖြစ်တဲ့ flaky test ရပါတယ်။ userEvent method တိုင်းကို await လုပ်ပြီး test function ကို async ထားပါ။

Query မတွေ့ရင် data-testid ထည့်လိုက်တာ

getByLabelText('Email') မတွေ့ရင် input မှာ label ချိတ်မထားလို့ ဖြစ်တတ်ပါတယ်။ data-testid ထည့်ပြီး ကျော်သွားရင် test က pass ဖြစ်ပေမယ့် screen reader user အတွက် bug က ဆက်ကျန်ပါတယ်။ <label htmlFor> ကို အရင် ပြင်ပါ။

Internal state ကို test လုပ်တာ

count state က 1 ဖြစ်လား၊ child component တစ်ခု render ဖြစ်လား ဆိုတာကို စစ်တဲ့ test တွေက useState ကို useReducer ပြောင်းရုံနဲ့ ကျိုးပါတယ်။ User ကတော့ ဘာမှ ကွာခြားမှု မမြင်ပါဘူး။ Screen ပေါ်က ရလဒ်ကိုသာ assert လုပ်ပါ။

Best Practices

ကောင်းမွန်တဲ့ အလေ့အကျင့်များ

  • Test နာမည်ကို user ရဲ့ ရှုထောင့်ကနေ ရေးပါ: "shows an error when the email is invalid"
  • Test တစ်ခုမှာ user flow တစ်ခုကိုသာ စစ်ပြီး arrange၊ act၊ assert ကို blank line နဲ့ ခွဲပါ
  • Loading၊ error၊ empty state တွေကိုလည်း happy path လိုပဲ test လုပ်ပါ
  • Release မတိုင်ခင် Tab၊ Enter၊ Space နဲ့ app ကို mouse မပါဘဲ တစ်ခေါက် သုံးကြည့်ပါ

Mini challenge

LoginForm အတွက် test နှစ်ခု

ပေးထားတဲ့ LoginForm အတွက် test နှစ်ခု ရေးပါ။ ပထမ test က email မမှန်ရင် alert ပေါ်ပြီး onLogin ကို မခေါ်ကြောင်း စစ်ပါ။ ဒုတိယ test က email မှန်ရင် space ဖြုတ်ထားတဲ့ email နဲ့ onLogin ကို ခေါ်ပြီး alert မပေါ်ကြောင်း စစ်ပါ။

  • Input ကို getByLabelText၊ button ကို getByRole နဲ့ နာမည်ဖြင့် ရှာပါ
  • userEvent.setup() ကို သုံးပြီး method တိုင်းကို await လုပ်ပါ
  • onLogin ကို vi.fn() နဲ့ ဖန်တီးပြီး ခေါ်/မခေါ်ကို စစ်ပါ
  • Alert မရှိခြင်းကို queryByRole နဲ့ စစ်ပါ
LoginForm.test.jsx
// LoginForm.jsx (given):
// <form noValidate onSubmit={handleSubmit}>
//   <label htmlFor="email">Email</label>
//   <input id="email" name="email" type="email" />
//   <button type="submit">Sign in</button>
//   {error && <p role="alert">{error}</p>}       // 'Enter a valid email.'
// </form>
// handleSubmit trims the email; no '@' -> sets error, else calls onLogin(email)

import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { expect, test, vi } from 'vitest';
import LoginForm from './LoginForm.jsx';

test('shows an alert for an invalid email', async () => {
  // TODO: set up user + onLogin mock, render, type 'not-an-email', click Sign in
  // TODO: expect the alert text, expect onLogin not to have been called
});

test('calls onLogin with a trimmed email', async () => {
  // TODO: type '  mya@example.com ', submit, check the call and no alert
});

ရလဒ်: npx vitest run က test နှစ်ခုလုံး pass ဖြစ်ကြောင်း ပြပါတယ်။ Label ကို input နဲ့ ချိတ်ထားတဲ့ htmlFor ကို ဖျက်ကြည့်ရင် getByLabelText ကြောင့် test နှစ်ခုလုံး fail ဖြစ်ပါတယ်။

အရိပ်အမြွက် ကြည့်ရန်

screen.getByRole('alert') ကို toHaveTextContent('Enter a valid email.') နဲ့ စစ်နိုင်ပါတယ်။ Mock function အတွက် expect(onLogin).not.toHaveBeenCalled() နဲ့ toHaveBeenCalledWith('mya@example.com') ကို သုံးပါ။

အဖြေ ကြည့်ရန်
LoginForm.test.jsx
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { expect, test, vi } from 'vitest';
import LoginForm from './LoginForm.jsx';

test('shows an alert for an invalid email', async () => {
  const user = userEvent.setup();
  const onLogin = vi.fn();
  render(<LoginForm onLogin={onLogin} />);

  await user.type(screen.getByLabelText('Email'), 'not-an-email');
  await user.click(screen.getByRole('button', { name: 'Sign in' }));

  expect(screen.getByRole('alert')).toHaveTextContent('Enter a valid email.');
  expect(onLogin).not.toHaveBeenCalled();
});

test('calls onLogin with a trimmed email', async () => {
  const user = userEvent.setup();
  const onLogin = vi.fn().mockResolvedValue(undefined);
  render(<LoginForm onLogin={onLogin} />);

  await user.type(screen.getByLabelText('Email'), '  mya@example.com ');
  await user.click(screen.getByRole('button', { name: 'Sign in' }));

  expect(onLogin).toHaveBeenCalledWith('mya@example.com');
  expect(screen.queryByRole('alert')).not.toBeInTheDocument();
});

Quick Quiz 1 / 3

Product card တစ်ခုထဲက "Delete" button ကို ရှာဖို့ ဘယ် query က အကောင်းဆုံးလဲ။

Quick Quiz 2 / 3

Form ကို မှန်ကန်စွာ submit လုပ်ပြီးရင် error alert မရှိရဘူးဆိုတာ စစ်ချင်တယ်။ ဘယ်ဟာ မှန်လဲ။

Quick Quiz 3 / 3

Component က mount ဖြစ်ပြီး fetch ပြီးမှ "3 results" ကို ပြပါတယ်။ Test ထဲမှာ ဘယ်လို စစ်သင့်လဲ။

မှတ်ထားရမယ့် အချက်များ

  • Test က user မြင်တဲ့ role၊ label၊ စာသားကို စစ်ပြီး class နာမည်နဲ့ state ကို မစစ်ပါ
  • Query ဦးစားပေး: getByRole → getByLabelText → ... → getByTestId (နောက်ဆုံး)
  • getBy က throw၊ queryBy က null၊ findBy က စောင့်ပေးတဲ့ promise ပါ
  • userEvent.setup() ကို သုံးပြီး method တိုင်းကို await လုပ်ပါ
  • Query ရှာမတွေ့ရင် component ရဲ့ accessibility ကို အရင်စစ်ပါ

React Testing Library Introduction — Testing Library

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။