Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Test the branch where persisted state suppresses the UI
Cover the case where stored state (localStorage/cookie/flag) hides or changes UI, not only the default first-visit path.
Bad example
| 1 | it('renders the popup', () => { |
| 2 | render(<Popup />); |
| 3 | expect(screen.getByRole('dialog')).toBeInTheDocument(); |
| 4 | }); |
| 5 | // Only the first-visit path is covered; the dismissed/seen branch is untested. |
Explanation (EN)
Only the default first-visit path is asserted. The branch where a stored flag should hide the popup is never exercised, so a regression there ships unnoticed.
Objašnjenje (HR)
Pokriven je samo zadani put prvog posjeta. Grana u kojoj spremljena zastavica treba sakriti popup nikad se ne testira, pa regresija ondje prolazi neprimijećeno.
Good example
| 1 | it('renders the popup on first visit', () => { |
| 2 | render(<Popup />); |
| 3 | expect(screen.getByRole('dialog')).toBeInTheDocument(); |
| 4 | }); |
| 5 |
|
| 6 | it('does not render when already dismissed', () => { |
| 7 | localStorage.setItem(LOCAL_STORAGE_KEY, 'true'); |
| 8 | render(<Popup />); |
| 9 | expect(screen.queryByRole('dialog')).not.toBeInTheDocument(); |
| 10 | }); |
Explanation (EN)
Both branches are tested: the first-visit render and the suppression case driven by persisted state, so either path breaking is caught.
Objašnjenje (HR)
Testirane su obje grane: prikaz pri prvom posjetu i slučaj potiskivanja vođen spremljenim stanjem, pa se kvar u bilo kojoj putanji uhvati.
Notes (EN)
Keep the persistence check testable at the unit it lives in — if the read happens in a wrapper/entry layer that the test can't reach, consider moving it into the component so the branch can be exercised.
Bilješke (HR)
Drži provjeru perzistencije testabilnom na razini gdje živi — ako se čitanje događa u omotaču/entry sloju koji test ne doseže, razmisli o premještanju u komponentu kako bi se grana mogla testirati.