Skip to content

Commit abdb144

Browse files
committed
test: migrate tests for real browser tests using vitest browsermode +MSW
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent 2d7a46d commit abdb144

15 files changed

Lines changed: 1046 additions & 266 deletions

.github/workflows/node-test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ jobs:
5151
CYPRESS_INSTALL_BINARY: 0
5252
run: |
5353
npm ci
54+
npx playwright install chromium
5455
npm run build --if-present
5556
5657
- name: Test

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ pids
1717
lib-cov
1818

1919
# Coverage directory used by tools like istanbul
20+
.vitest*
21+
__screenshots__/
2022
coverage
2123

2224
# nyc test coverage

REUSE.toml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ SPDX-PackageSupplier = "Nextcloud <info@nextcloud.com>"
66
SPDX-PackageDownloadLocation = "https://github.com/nextcloud-libraries/nextcloud-axios"
77

88
[[annotations]]
9-
path = ["package-lock.json", "package.json", "tsconfig.json"]
9+
path = ["package-lock.json", "package.json", "tsconfig.json", "test/tsconfig.json"]
1010
precedence = "aggregate"
1111
SPDX-FileCopyrightText = "2018-2024 Nextcloud GmbH and Nextcloud contributors"
1212
SPDX-License-Identifier = "GPL-3.0-or-later"
13-
14-
[[annotations]]
15-
path = ".eslintrc.json"
16-
precedence = "aggregate"
17-
SPDX-FileCopyrightText = "2023 Nextcloud GmbH and Nextcloud contributors"
18-
SPDX-License-Identifier = "GPL-3.0-or-later"

lib/client.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,30 @@ import { getRequestToken, onRequestTokenUpdate } from '@nextcloud/auth'
99
import Axios from 'axios'
1010

1111
export interface CancelableAxiosInstance extends AxiosInstance {
12+
/**
13+
* @deprecated - use the AbortController API instead
14+
*/
1215
CancelToken: CancelTokenStatic
1316
isCancel: typeof Axios.isCancel
1417
}
1518

16-
const client = Axios.create({
17-
headers: {
18-
requesttoken: getRequestToken() ?? '',
19-
'X-Requested-With': 'XMLHttpRequest',
20-
},
21-
})
19+
/**
20+
* Get an Axios instance with default Nextcloud headers and CSRF token handling.
21+
*/
22+
export function getCancelableClient(): CancelableAxiosInstance {
23+
const client = Axios.create({
24+
headers: {
25+
requesttoken: getRequestToken() ?? '',
26+
'X-Requested-With': 'XMLHttpRequest',
27+
},
28+
})
2229

23-
onRequestTokenUpdate((token: string) => {
24-
client.defaults.headers.requesttoken = token
25-
})
30+
onRequestTokenUpdate((token: string) => {
31+
client.defaults.headers.requesttoken = token
32+
})
2633

27-
export const cancelableClient: CancelableAxiosInstance = Object.assign(client, {
28-
CancelToken: Axios.CancelToken,
29-
isCancel: Axios.isCancel,
30-
})
34+
return Object.assign(client, {
35+
CancelToken: Axios.CancelToken,
36+
isCancel: Axios.isCancel,
37+
})
38+
}

lib/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
* SPDX-License-Identifier: GPL-3.0-or-later
44
*/
55

6-
import { cancelableClient } from './client.ts'
6+
import { getCancelableClient } from './client.ts'
77
import { onCsrfTokenError } from './interceptors/csrf-token.ts'
88
import { onMaintenanceModeError } from './interceptors/maintenance-mode.ts'
99
import { onNotLoggedInError } from './interceptors/not-logged-in.ts'
1010

11+
const cancelableClient = getCancelableClient()
1112
cancelableClient.interceptors.response.use((r) => r, onCsrfTokenError(cancelableClient))
1213
cancelableClient.interceptors.response.use((r) => r, onMaintenanceModeError(cancelableClient))
1314
cancelableClient.interceptors.response.use((r) => r, onNotLoggedInError)

lib/interceptors/not-logged-in.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ export async function onNotLoggedInError(error: unknown) {
2121
if (status === 401
2222
&& response?.data?.message === 'Current user is not logged in'
2323
&& config?.reloadExpiredSession
24-
&& window?.location) {
24+
&& globalThis.location?.reload) {
2525
console.error(`Request to ${responseURL} failed because the user session expired. Reloading the page …`)
26-
27-
window.location.reload()
26+
if (globalThis.OC?.reload) {
27+
globalThis.OC.reload()
28+
} else {
29+
globalThis.location.reload()
30+
}
2831
}
2932
}
3033

0 commit comments

Comments
 (0)