Skip to content

Commit

Permalink
Fix JS unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Aurélien Bompard <aurelien@bompard.org>
  • Loading branch information
abompard committed Jan 8, 2024
1 parent eebb4c8 commit d3a20dd
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ jobs:
npm run type-check
popd
- name: run type-check
- name: run unit tests
run: |
pushd frontend/
npm run test:unit -- run
Expand Down
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "fmn",
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite --host 0.0.0.0",
"build": "run-p type-check build-only",
Expand Down
1 change: 1 addition & 0 deletions frontend/src/auth/LoginFedora.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import router from "../router";
import LoginFedora from "./LoginFedora.vue";
import type Authenticator from "./authenticator";

window.scrollTo = vi.fn();
vi.mock("@/api");

describe("LoginFedora", () => {
Expand Down
12 changes: 9 additions & 3 deletions frontend/src/auth/authenticator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,15 @@ describe("authenticator", () => {
});
const state = await getStateFromStorage();
// Now check the redirect
expect(window.location.assign).toHaveBeenCalledWith(
`https://id.example.test/auth?redirect_uri=%2Foidc-callback&client_id=dummy-client-id&response_type=code&state=${state}&scope=dummy-scope`,
);
const expected = `https://id.example.test/auth?redirect_uri=%2Foidc-callback&client_id=dummy-client-id&response_type=code&state=${state}&scope=dummy-scope`;
// We can't use toHaveBeenCalledWith() because depending on the platform's features the
// appauth library will or will not use PKCE and thus add a random code_challenge to the
// query string.
// expect(window.location.assign).toHaveBeenCalledWith(expected);
expect(window.location.assign).toHaveBeenCalledTimes(1);
const redirectCall = vi.mocked(window.location.assign).mock.lastCall;
expect(redirectCall).toHaveLength(1);
expect(redirectCall[0]).toContain(expected);
});

it("throws on authorization requests without config", async () => {
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/auth/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { useUserStore } from "../stores/user";
import type Authenticator from "./authenticator";
import { login, logout, useAuth } from "./index";

window.scrollTo = vi.fn();

const loginUser = (userStore: ReturnType<typeof useUserStore>) => {
userStore.$patch({
accessToken: "testing",
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/AdminLink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useUserStore } from "../stores/user";
import AdminLink from "./AdminLink.vue";
import { loginAdmin, loginUser, render } from "./test-utils";

window.scrollTo = vi.fn();
vi.mock("../auth");

describe("AdminLink", () => {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/LoginButton.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { useUserStore } from "../stores/user";
import LoginButton from "./LoginButton.vue";
import { loginUser, render } from "./test-utils";

window.scrollTo = vi.fn();
vi.mock("../auth");

describe("LoginButton", () => {
Expand Down
11 changes: 8 additions & 3 deletions frontend/src/components/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
//
// SPDX-License-Identifier: MIT

import type { useUserStore } from "../stores/user";
import type { Destination } from "@/api/types";
import { vctooltip } from "@coreui/bootstrap-vue";
import { render as baseRender } from "@testing-library/vue";
import { getActivePinia, type Pinia } from "pinia";
import { vi } from "vitest";
import type { Component } from "vue";
import { createI18n } from "vue-i18n";
import router from "../router";
import { vi } from "vitest";
import type { Destination } from "@/api/types";
import type { useUserStore } from "../stores/user";

export const loginUser = (userStore: ReturnType<typeof useUserStore>) => {
userStore.$patch({
Expand Down Expand Up @@ -47,6 +48,10 @@ export const render = (
plugins: [router, pinia, i18n],
provide: {
auth: vi.fn(),
icons: {},
},
directives: {
"c-tooltip": vctooltip,
},
},
});
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/stores/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import router from "../router";
import { useUserStore } from "./user";

window.scrollTo = vi.fn();

describe("User Store", () => {
beforeEach(async () => {
await router.replace("/");
Expand Down

0 comments on commit d3a20dd

Please sign in to comment.