Skip to content

Commit d5a64d4

Browse files
author
Karol Tatała
committed
IW-4876 | tests and more fixes
1 parent e0687ec commit d5a64d4

File tree

5 files changed

+286
-182
lines changed

5 files changed

+286
-182
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"@babel/preset-react": "^7.8.3",
6464
"@rollup/plugin-commonjs": "^11.0.2",
6565
"@rollup/plugin-node-resolve": "^7.1.1",
66+
"@testing-library/react": "^11.2.2",
6667
"babel-core": "^7.0.0-bridge.0",
6768
"babel-eslint": "^10.1.0",
6869
"babel-jest": "^25.1.0",

source/components/GlobalNavigation/components/Notifications/CardText.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ function bold(text) {
1919
}
2020

2121
function escapeHtml(unsafe) {
22+
if (!unsafe) {
23+
return unsafe;
24+
}
25+
2226
return unsafe
2327
.replace(/&/g, '&')
2428
.replace(/</g, '&lt;')
@@ -159,9 +163,9 @@ function getArticleCommentReplyAtMentionMessageBody(t, { latestActors, title })
159163
}
160164

161165
const getText = (translateFunc, model, userData) => {
162-
const { type, snippet, title, totalUniqueActors, latestActors, refersToAuthorId } = model;
163-
const escapedTitle = escapeHtml(title);
164-
const postTitleMarkup = `<b>${escapedTitle}</b>`;
166+
const { type, snippet, title: dangerousTitle, totalUniqueActors, latestActors, refersToAuthorId } = model;
167+
const title = escapeHtml(dangerousTitle);
168+
const postTitleMarkup = `<b>${title}</b>`;
165169

166170
if (isAnnouncement(type)) {
167171
return snippet;

source/components/GlobalNavigation/components/Notifications/CardText.spec.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import ShallowRenderer from 'react-test-renderer/shallow';
33
import merge from 'lodash/merge';
4+
import { render } from '@testing-library/react';
45

56
import { notificationTypes } from '../../models/notificationTypes';
67
import { useUserData } from '../../context/UserContext';
@@ -10,7 +11,7 @@ import CardText from './CardText';
1011
const actorsMock = [
1112
{
1213
id: '12345',
13-
name: 'ATOB',
14+
name: 'ATO<b>B</b>',
1415
avatarUrl: 'https://static.wikia.nocookie.net/458a839b-8f84-42c2-b9d1-cf8c5a21bd75',
1516
profileUrl: 'http://localhost:6060/wiki/User:ATOB',
1617
src: 'https://static.wikia.nocookie.net/458a839b-8f84-42c2-b9d1-cf8c5a21bd75',
@@ -35,6 +36,13 @@ jest.mock('../../context/UserContext', () => ({
3536
useUserData: jest.fn(),
3637
}));
3738

39+
jest.mock('react-i18next', () => ({
40+
...jest.requireActual('react-i18next'),
41+
useTranslation: () => [jest.fn((key, params) => {
42+
return `${key}${params ? JSON.stringify(params) : ''}`;
43+
})],
44+
}));
45+
3846
const defaultProps = {
3947
track: () => null,
4048
model: {
@@ -45,20 +53,19 @@ const defaultProps = {
4553
latestEventUri: 'http://xkxd.wikia.com/d/p/3100000000000001096/r/3086787452863005635',
4654
snippet: 'some snippet',
4755
timestamp: 1550663179,
48-
title: 'MOAR LIKES',
56+
title: 'MOAR <b>LIKES</b>',
4957
type: notificationTypes.discussionReply,
5058
totalUniqueActors: actorsMock.length,
5159
uri: 'http://xkxd.wikia.com/d/p/3100000000000001096',
5260
},
5361
};
5462

5563
function renderComponent(props) {
56-
const renderer = new ShallowRenderer();
5764
const computedProps = merge({}, defaultProps, props);
5865

59-
renderer.render(<CardText {...computedProps} />);
66+
const {container} = render(<CardText {...computedProps} />);
6067

61-
return renderer.getRenderOutput();
68+
return container.firstChild;
6269
}
6370

6471
test('CardText renders correctly with default props', () => {
@@ -223,7 +230,7 @@ describe('Discussion at mentions', () => {
223230
type: notificationTypes.postAtMention,
224231
latestActors: actorsMock.slice(0, 1),
225232
totalUniqueActors: 1,
226-
title: 'Post with at mention',
233+
title: 'Post with at <b>mention</b>',
227234
},
228235
})).toMatchSnapshot();
229236
});
@@ -234,7 +241,7 @@ describe('Discussion at mentions', () => {
234241
type: notificationTypes.threadAtMention,
235242
latestActors: actorsMock.slice(0, 1),
236243
totalUniqueActors: 1,
237-
title: 'Post with at mention',
244+
title: 'Post with at <b>mention</b>',
238245
},
239246
})).toMatchSnapshot();
240247
});
@@ -250,7 +257,7 @@ describe('Article Comments', () => {
250257
type: notificationTypes.articleCommentReply,
251258
latestActors: actorsMock.slice(0, 1),
252259
totalUniqueActors: 1,
253-
title: 'Article Title',
260+
title: 'Article <b>Title</b>',
254261
refersToAuthorId: someMockAuthor.id,
255262
},
256263
})).toMatchSnapshot();
@@ -265,7 +272,7 @@ describe('Article Comments', () => {
265272
type: notificationTypes.articleCommentReply,
266273
latestActors: actorsMock.slice(0, 1),
267274
totalUniqueActors: 1,
268-
title: 'Article Title',
275+
title: 'Article <b>Title</b>',
269276
refersToAuthorId: '11111',
270277
},
271278
})).toMatchSnapshot();
@@ -283,7 +290,7 @@ describe('Article Comments at mentions', () => {
283290
type: notificationTypes.articleCommentAtMention,
284291
latestActors: actorsMock.slice(0, 1),
285292
totalUniqueActors: 1,
286-
title: 'Article Title',
293+
title: 'Article <b>Title</b>',
287294
},
288295
})).toMatchSnapshot();
289296
});
@@ -294,7 +301,7 @@ describe('Article Comments at mentions', () => {
294301
type: notificationTypes.articleCommentReplyAtMention,
295302
latestActors: actorsMock.slice(0, 1),
296303
totalUniqueActors: 1,
297-
title: 'Article Title',
304+
title: 'Article <b>Title</b>',
298305
},
299306
})).toMatchSnapshot();
300307
});
@@ -305,7 +312,7 @@ describe('Article Comments at mentions', () => {
305312
type: notificationTypes.articleCommentReplyAtMention,
306313
latestActors: [{ id: 0, name: null }],
307314
totalUniqueActors: 1,
308-
title: 'Article Title',
315+
title: 'Article <b>Title</b>',
309316
},
310317
})).toMatchSnapshot();
311318
});

0 commit comments

Comments
 (0)