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).
Use window.location.assign() for programmatic navigation so it can be mocked in tests
Navigate via window.location.assign(url) rather than assigning window.location.href, because the method can be spied/mocked to prevent real navigation in tests.
Bad example
| 1 | function redirectToLogin() { |
| 2 | window.location.href = '/login'; |
| 3 | } |
Explanation (EN)
Assigning to `location.href` is a property write that is awkward to intercept; in browser-mode or jsdom tests it can trigger a real navigation that breaks the test runner.
Objašnjenje (HR)
Pridruzivanje vrijednosti `location.href` je upis u svojstvo koje je nezgodno presresti; u testovima u browser nacinu rada ili jsdom-u moze pokrenuti stvarnu navigaciju koja srusi test runner.
Good example
| 1 | function redirectToLogin() { |
| 2 | window.location.assign('/login'); |
| 3 | } |
| 4 |
|
| 5 | // in a test: |
| 6 | const assign = vi.spyOn(window.location, 'assign').mockImplementation(() => {}); |
| 7 | redirectToLogin(); |
| 8 | expect(assign).toHaveBeenCalledWith('/login'); |
Explanation (EN)
`assign` is a method, so tests can spy on it and replace it with a no-op, asserting the intended URL without ever navigating for real.
Objašnjenje (HR)
`assign` je metoda pa je testovi mogu spijunirati i zamijeniti no-op implementacijom, provjeravajuci zeljeni URL bez stvarne navigacije.