Skip to content

Commit 28cb1af

Browse files
committed
add frontend test
1 parent 8cda74d commit 28cb1af

5 files changed

Lines changed: 145 additions & 26 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = require('@flarum/jest-config')({
2+
moduleNameMapper: {
3+
'^flarum/(.*)$': '<rootDir>/../../../framework/core/js/src/$1',
4+
},
5+
});
Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
{
2-
"private": true,
3-
"name": "@flarum/mentions",
4-
"version": "0.0.0",
5-
"prettier": "@flarum/prettier-config",
6-
"devDependencies": {
7-
"prettier": "^2.5.1",
8-
"flarum-webpack-config": "^3.0.0",
9-
"webpack": "^5.104.1",
10-
"webpack-cli": "^4.9.1",
11-
"@flarum/prettier-config": "^1.0.0",
12-
"flarum-tsconfig": "^2.0.0",
13-
"typescript": "^4.5.4",
14-
"typescript-coverage-report": "^0.6.1"
15-
},
16-
"scripts": {
17-
"dev": "webpack --mode development --watch",
18-
"build": "webpack --mode production",
19-
"analyze": "cross-env ANALYZER=true yarn run build",
20-
"format": "prettier --write src",
21-
"format-check": "prettier --check src",
22-
"clean-typings": "npx rimraf dist-typings && mkdir dist-typings",
23-
"build-typings": "yarn run clean-typings && ([ -e src/@types ] && cp -r src/@types dist-typings/@types || true) && tsc && yarn run post-build-typings",
24-
"post-build-typings": "find dist-typings -type f -name '*.d.ts' -print0 | xargs -0 sed -i 's,../src/@types,@types,g'",
25-
"check-typings": "tsc --noEmit --emitDeclarationOnly false",
26-
"check-typings-coverage": "typescript-coverage-report"
27-
}
2+
"private": true,
3+
"name": "@flarum/mentions",
4+
"version": "0.0.0",
5+
"prettier": "@flarum/prettier-config",
6+
"devDependencies": {
7+
"prettier": "^2.5.1",
8+
"flarum-webpack-config": "^3.0.0",
9+
"webpack": "^5.104.1",
10+
"webpack-cli": "^4.9.1",
11+
"@flarum/jest-config": "^2.0.0",
12+
"@flarum/prettier-config": "^1.0.0",
13+
"flarum-tsconfig": "^2.0.0",
14+
"typescript": "^4.5.4",
15+
"typescript-coverage-report": "^0.6.1"
16+
},
17+
"scripts": {
18+
"dev": "webpack --mode development --watch",
19+
"build": "webpack --mode production",
20+
"analyze": "cross-env ANALYZER=true yarn run build",
21+
"test": "yarn node --experimental-vm-modules $(yarn bin jest)",
22+
"format": "prettier --write src",
23+
"format-check": "prettier --check src",
24+
"clean-typings": "npx rimraf dist-typings && mkdir dist-typings",
25+
"build-typings": "yarn run clean-typings && ([ -e src/@types ] && cp -r src/@types dist-typings/@types || true) && tsc && yarn run post-build-typings",
26+
"post-build-typings": "find dist-typings -type f -name '*.d.ts' -print0 | xargs -0 sed -i 's,../src/@types,@types,g'",
27+
"check-typings": "tsc --noEmit --emitDeclarationOnly false",
28+
"check-typings-coverage": "typescript-coverage-report"
29+
},
30+
"type": "module"
2831
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import bootstrapForum from '@flarum/jest-config/src/bootstrap/forum';
2+
import app from 'flarum/forum/app';
3+
import Notification from 'flarum/common/models/Notification';
4+
import PostMentionedNotification from '../../../../src/forum/components/PostMentionedNotification';
5+
import { dirname, resolve } from 'path';
6+
import { fileURLToPath } from 'url';
7+
8+
const coreJsDir = resolve(dirname(fileURLToPath(import.meta.url)), '../../../../../../../framework/core/js');
9+
10+
beforeAll(() => {
11+
const cwd = process.cwd();
12+
13+
try {
14+
process.chdir(coreJsDir);
15+
bootstrapForum();
16+
} finally {
17+
process.chdir(cwd);
18+
}
19+
});
20+
21+
describe('PostMentionedNotification', () => {
22+
beforeAll(() => {
23+
app.boot();
24+
25+
app.store.pushPayload({
26+
data: [
27+
{
28+
id: '1',
29+
type: 'discussions',
30+
attributes: {
31+
slug: '1-original-discussion',
32+
title: 'Original discussion',
33+
},
34+
},
35+
{
36+
id: '2',
37+
type: 'discussions',
38+
attributes: {
39+
slug: '2-reply-discussion',
40+
title: 'Reply discussion',
41+
},
42+
},
43+
],
44+
});
45+
46+
app.store.pushPayload({
47+
data: {
48+
id: '10',
49+
type: 'posts',
50+
attributes: {
51+
number: 3,
52+
contentHtml: '<p>Original post</p>',
53+
},
54+
relationships: {
55+
discussion: {
56+
data: { type: 'discussions', id: '1' },
57+
},
58+
},
59+
},
60+
});
61+
});
62+
63+
function makeNotification(content: Record<string, unknown>) {
64+
return new Notification(
65+
{
66+
id: '1',
67+
type: 'notifications',
68+
attributes: {
69+
content,
70+
contentType: 'postMentioned',
71+
createdAt: new Date(),
72+
isRead: false,
73+
},
74+
relationships: {
75+
subject: {
76+
data: { type: 'posts', id: '10' },
77+
},
78+
},
79+
},
80+
app.store
81+
);
82+
}
83+
84+
function hrefFor(content: Record<string, unknown>) {
85+
const component = new PostMentionedNotification();
86+
87+
component.attrs = {
88+
notification: makeNotification(content),
89+
} as any;
90+
91+
return component.href();
92+
}
93+
94+
it.each([
95+
{ content: { replyNumber: 5, discussionId: 2 }, expectedHref: '/d/2/5' },
96+
{ content: { replyNumber: 5 }, expectedHref: '/d/1-original-discussion/5' },
97+
{ content: { replyNumber: 1, discussionId: 2 }, expectedHref: '/d/2' },
98+
])('returns $expectedHref for notification content $content', ({ content, expectedHref }) => {
99+
expect(hrefFor(content)).toBe(expectedHref);
100+
});
101+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"include": ["tests/**/*"],
4+
"files": ["../../../node_modules/@flarum/jest-config/shims.d.ts"],
5+
"compilerOptions": {
6+
"strict": false,
7+
"noImplicitReturns": false,
8+
"noImplicitAny": false
9+
}
10+
}

0 commit comments

Comments
 (0)