diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 23aeb691305d..05cea772f65b 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -4,6 +4,8 @@ ### Checks +- [ ] Adding/modifying Typescript code? + - [ ] I have used `qs`,`qsa` or `qsr` instead of JQuery selectors. - [ ] Adding quotes? - [ ] Make sure to include translations for the quotes in the description (or another comment) so we can verify their content. - [ ] Adding a language? diff --git a/docs/CONTRIBUTING_ADVANCED.md b/docs/CONTRIBUTING_ADVANCED.md index b762aab97988..13c5d774c6d1 100644 --- a/docs/CONTRIBUTING_ADVANCED.md +++ b/docs/CONTRIBUTING_ADVANCED.md @@ -155,6 +155,8 @@ If you are on a UNIX system and you get a spawn error, run npm with `sudo`. Code formatting is enforced by [Prettier](https://prettier.io/docs/en/install.html), which automatically runs every time you make a commit. +We are currently in the process of converting from JQuery to vanilla JS. When submitting new code, please use the `qs`, `qsa` and `qsr` helper functions. These return a class with a lot of JQuery-like methods. You can read how they work and import them from `frontend/src/ts/utils/dom.ts`. + For guidelines on commit messages, adding themes, languages, or quotes, please refer to [CONTRIBUTING.md](./CONTRIBUTING.md). Following these guidelines will increase the chances of getting your change accepted. ## Questions diff --git a/frontend/__tests__/setup-tests.ts b/frontend/__tests__/setup-tests.ts index ce5a30cf562a..43b2255ae136 100644 --- a/frontend/__tests__/setup-tests.ts +++ b/frontend/__tests__/setup-tests.ts @@ -18,6 +18,44 @@ vi.mock("../src/ts/firebase", () => ({ isAuthenticated: () => false, })); -const input = document.createElement("input"); -input.id = "wordsInput"; -document.body.appendChild(input); +vi.mock("../src/ts/utils/dom", () => { + const createMockElement = (): any => { + const mock = { + qsr: vi.fn(), + qs: vi.fn().mockReturnValue(null), + find: vi.fn(), + addClass: vi.fn(), + removeClass: vi.fn(), + hide: vi.fn(), + show: vi.fn(), + setText: vi.fn(), + prependHtml: vi.fn(), + empty: vi.fn(), + appendHtml: vi.fn(), + native: document.createElement("div"), + }; + + // Make chainable methods return the mock itself + mock.qsr.mockImplementation(() => createMockElement()); + mock.addClass.mockReturnValue(mock); + mock.removeClass.mockReturnValue(mock); + mock.hide.mockReturnValue(mock); + mock.show.mockReturnValue(mock); + mock.setText.mockReturnValue(mock); + mock.prependHtml.mockReturnValue(mock); + mock.empty.mockReturnValue(mock); + + return mock; + }; + + return { + qsr: vi.fn().mockImplementation(() => createMockElement()), + qs: vi.fn().mockImplementation(() => createMockElement()), + qsa: vi.fn().mockReturnValue([]), + }; +}); + +// Mock document.querySelector to return a div +global.document.querySelector = vi + .fn() + .mockReturnValue(document.createElement("div")); diff --git a/frontend/src/index.html b/frontend/src/index.html index a8491bf061dc..c514c2b85840 100644 --- a/frontend/src/index.html +++ b/frontend/src/index.html @@ -10,7 +10,7 @@
-