Skip to content

Commit 973de64

Browse files
committed
test: add nuxt tests
1 parent 6f4e536 commit 973de64

File tree

5 files changed

+516
-0
lines changed

5 files changed

+516
-0
lines changed

Diff for: packages/nuxt/package.json

+5
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,24 @@
2525
"dev": "nuxi dev playground",
2626
"dev:prepare": "nuxt-module-build prepare && nuxi prepare playground",
2727
"build": "nuxt-module-build prepare && nuxt-module-build build",
28+
"test": "vitest",
2829
"check": "tsc --noEmit",
2930
"lint": "eslint --ext .ts src",
3031
"prepack": "pnpm build"
3132
},
3233
"devDependencies": {
3334
"@nuxt/module-builder": "^0.5.5",
35+
"@nuxt/test-utils": "^3.11.0",
3436
"@silverhand/eslint-config": "^5.0.0",
37+
"@vue/test-utils": "^2.4.4",
3538
"eslint": "^8.56.0",
3639
"h3": "^1.10.2",
40+
"happy-dom": "^13.4.1",
3741
"lint-staged": "^15.0.0",
3842
"nuxt": "^3.10.2",
3943
"prettier": "^3.0.0",
4044
"typescript": "^5.3.3",
45+
"vitest": "^1.3.1",
4146
"vue": "^3.4.19"
4247
},
4348
"eslintConfig": {

Diff for: packages/nuxt/test/event-handler.test.ts

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { ServerResponse, IncomingMessage } from 'node:http';
2+
import { Socket } from 'node:net';
3+
4+
import * as logtoNode from '@logto/node';
5+
import { mockNuxtImport } from '@nuxt/test-utils/runtime';
6+
import { createEvent } from 'h3';
7+
import { describe, expect, it, vi, type Mock } from 'vitest';
8+
9+
import handler from '@/src/runtime/server/event-handler';
10+
11+
mockNuxtImport('useRuntimeConfig', () =>
12+
vi.fn(() => ({
13+
logto: {
14+
cookieEncryptionKey: 'foo',
15+
pathnames: {
16+
signIn: '/sign-in',
17+
signOut: '/sign-out',
18+
callback: '/callback',
19+
},
20+
},
21+
}))
22+
);
23+
24+
const cookies = new Map();
25+
const getRequestURL = vi.fn(() => new URL('http://localhost:3000'));
26+
const sendRedirect = vi.fn();
27+
28+
vi.stubGlobal('defineEventHandler', (handler: unknown) => handler);
29+
vi.stubGlobal('getRequestURL', getRequestURL);
30+
vi.stubGlobal('getCookie', (_event: unknown, name: string) => cookies.get(name));
31+
vi.stubGlobal('setCookie', (_event: unknown, name: string, value: string) => {
32+
cookies.set(name, value);
33+
});
34+
vi.stubGlobal('sendRedirect', sendRedirect);
35+
36+
const LogtoClient = vi.fn();
37+
38+
vi.spyOn(logtoNode, 'default', 'get').mockImplementation(() => {
39+
/* eslint-disable @silverhand/fp/no-mutation */
40+
LogtoClient.prototype.signIn = vi.fn();
41+
LogtoClient.prototype.isAuthenticated = vi.fn().mockResolvedValue(false);
42+
LogtoClient.prototype.handleSignInCallback = vi.fn();
43+
/* eslint-enable @silverhand/fp/no-mutation */
44+
return LogtoClient;
45+
});
46+
47+
const createH3Event = () => {
48+
const incoming = new IncomingMessage(new Socket());
49+
const response = new ServerResponse(incoming);
50+
return createEvent(incoming, response);
51+
};
52+
53+
describe('event-handler', async () => {
54+
it('should inject logto client', async () => {
55+
const event = createH3Event();
56+
await handler(event);
57+
58+
expect(event.context.logtoClient).toBeInstanceOf(LogtoClient);
59+
});
60+
61+
it('should handle sign-in', async () => {
62+
const event = createH3Event();
63+
getRequestURL.mockReturnValueOnce(new URL('http://localhost:3000/sign-in'));
64+
await handler(event);
65+
expect(LogtoClient.prototype.signIn).toHaveBeenCalledWith('http://localhost:3000/callback');
66+
});
67+
68+
it('should handle callback with custom callback pathname', async () => {
69+
const event = createH3Event();
70+
// @ts-expect-error
71+
(useRuntimeConfig as Mock<typeof useRuntimeConfig>).mockReturnValueOnce({
72+
logto: {
73+
postCallbackRedirectUri: '/',
74+
cookieEncryptionKey: 'foo',
75+
pathnames: {
76+
signIn: '/sign-in',
77+
signOut: '/sign-out',
78+
callback: '/callback-1',
79+
},
80+
},
81+
});
82+
getRequestURL.mockReturnValueOnce(new URL('http://localhost:3000/callback-1'));
83+
await handler(event);
84+
expect(LogtoClient.prototype.handleSignInCallback).toHaveBeenCalledWith(
85+
'http://localhost:3000/callback-1'
86+
);
87+
expect(sendRedirect).toHaveBeenCalledWith(event, '/', 302);
88+
});
89+
});

Diff for: packages/nuxt/test/setup.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { vitest } from 'vitest';
2+
3+
vitest.stubGlobal('defineEventHandler', (handler: unknown) => handler);

Diff for: packages/nuxt/vitest.config.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defineVitestConfig } from '@nuxt/test-utils/config';
2+
3+
export default defineVitestConfig({
4+
test: {
5+
environment: 'nuxt',
6+
setupFiles: ['test/setup.ts'],
7+
},
8+
});

0 commit comments

Comments
 (0)