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 real behavior, don't fake the primitive you're verifying
Establish the real precondition (scroll, layout, state) and let the code react, instead of mocking the exact API the code depends on to force an outcome.
Bad example
| 1 | // Faking layout to force the sticky state |
| 2 | vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect') |
| 3 | .mockImplementation(function (this: HTMLElement) { |
| 4 | if (this.tagName === 'HEADER') { |
| 5 | return { top: -10, /* ... */ } as DOMRect; |
| 6 | } |
| 7 | return original.call(this); |
| 8 | }); |
| 9 |
|
| 10 | await render(<Header />); |
| 11 | // asserts sticky — but only because we faked the measurement |
Explanation (EN)
The test passes only because it mocks getBoundingClientRect, the exact API the feature reads. It verifies the mock, not the feature, and shatters if the implementation reads layout differently.
Objašnjenje (HR)
Test prolazi samo zato sto mockira getBoundingClientRect, bas onaj API koji znacajka cita. Provjerava mock, a ne znacajku, i raspada se ako implementacija mjeri layout drukcije.
Good example
| 1 | // Set up the real precondition, then render |
| 2 | document.body.style.minHeight = '300vh'; |
| 3 | window.scrollTo({ top: 200, behavior: 'instant' }); |
| 4 |
|
| 5 | await render(<Header />); |
| 6 |
|
| 7 | await expect.element(page.getByTestId('header-bar')).toHaveClass('fixed'); |
Explanation (EN)
By scrolling the real window before rendering, the component computes its own layout and the test stays valid regardless of how the feature internally measures position.
Objašnjenje (HR)
Skrolanjem stvarnog prozora prije renderiranja, komponenta sama racuna svoj layout i test ostaje valjan bez obzira na to kako znacajka interno mjeri poziciju.
Exceptions / Tradeoffs (EN)
Mocking is appropriate for external/non-deterministic dependencies (network, time, randomness) or APIs the environment cannot reproduce, not for the primitive whose effect you are asserting.
Iznimke / Tradeoffi (HR)
Mockiranje je prikladno za vanjske/nedeterministicke ovisnosti (mreza, vrijeme, slucajnost) ili API-je koje okruzenje ne moze reproducirati, ne za primitiv ciji ucinak tvrdis.