Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
Added ts-jest
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverlloyd committed Jan 8, 2021
1 parent a7585a7 commit 96d919e
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 34 deletions.
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-visualizer": "^4.0.4",
"storybook-chromatic": "^4.0.2",
"ts-jest": "^26.4.4",
"typescript": "^4.1.3"
},
"scripts": {
Expand Down Expand Up @@ -137,6 +138,23 @@
},
"jest": {
"testEnvironment": "jest-environment-jsdom-sixteen",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest"
},
"globals": {
"ts-jest": {
"diagnostics": false,
"tsConfigFile": "tsconfig.json"
}
},
"testMatch": [
"**/*.test.+(ts|tsx|js)"
],
"moduleNameMapper": {
"^@guardian/src-foundations/(.*)(?<!cjs)$": "@guardian/src-foundations/$1/cjs"
},
Expand Down
13 changes: 8 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,13 @@ export const App = ({
setLoading(true);
getDiscussion(shortUrl, { ...filters, page }).then((json) => {
setLoading(false);
if (json?.status !== 'error') {
setComments(json?.discussion?.comments);
setCommentCount(json?.discussion?.topLevelCommentCount);
if (json && json.status !== 'error') {
setComments(json && json.discussion && json.discussion.comments);
setCommentCount(
json && json.discussion && json.discussion.topLevelCommentCount,
);
}
setTotalPages(json?.pages);
setTotalPages(json && json.pages);
});
}, [filters, page, shortUrl]);

Expand Down Expand Up @@ -322,7 +324,8 @@ export const App = ({
};

const onPageChange = (page: number) => {
document.getElementById('comment-filters')?.scrollIntoView();
const element = document.getElementById('comment-filters');
element && element.scrollIntoView();
setPage(page);
};

Expand Down
9 changes: 6 additions & 3 deletions src/components/CommentContainer/CommentContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ export const CommentContainer = ({

const decideShowMoreText = () => {
const remainingResponses =
comment.metaData?.responseCount && comment.metaData?.responseCount - 3;
comment.metaData &&
comment.metaData.responseCount &&
comment.metaData.responseCount - 3;
if (remainingResponses === 1) return `Show 1 more reply`;
return `Show ${remainingResponses} more replies`;
};
Expand Down Expand Up @@ -160,8 +162,9 @@ export const CommentContainer = ({
))}
</ul>
{!expanded &&
comment.metaData?.responseCount &&
comment.metaData?.responseCount > 3 && (
comment.metaData &&
comment.metaData.responseCount &&
comment.metaData.responseCount > 3 && (
<div
className={cx(
topBorder,
Expand Down
6 changes: 3 additions & 3 deletions src/components/CommentForm/CommentForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export const CommentForm = ({
if (!body) return;

try {
const preview = onPreview ?? defaultPreview;
const preview = onPreview || defaultPreview;
const response = await preview(body);
setPreviewBody(response);
setShowPreview(true);
Expand All @@ -251,8 +251,8 @@ export const CommentForm = ({
setInfo('');

if (body) {
const comment = onComment ?? defaultComment;
const reply = onReply ?? defaultReply;
const comment = onComment || defaultComment;
const reply = onReply || defaultReply;
const response: CommentResponse = commentBeingRepliedTo
? await reply(shortUrl, body, commentBeingRepliedTo.id)
: await comment(shortUrl, body);
Expand Down
2 changes: 1 addition & 1 deletion src/components/FirstCommentWelcome/FirstCommentWelcome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const FirstCommentWelcome = ({
useEffect(() => {
const fetchShowPreview = async () => {
try {
const preview = onPreview ?? defaultPreview;
const preview = onPreview || defaultPreview;
const response = await preview(body);
setPreviewBody(response);
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/RecommendationCount/RecommendationCount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const RecommendationCount = ({
const newCount = count + 1;
setCount(newCount);
setRecommended(true);
const recommend = onRecommend ?? recommendDefault;
const recommend = onRecommend || recommendDefault;

recommend(commentId).then((accepted) => {
if (!accepted) {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/dateFormatter.js → src/lib/dateFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// date.getMonth() gets months from index 0
import { format } from 'timeago.js';

const monthConverter = {
const monthConverter: { [key: number]: string } = {
0: 'Jan',
1: 'Feb',
2: 'Mar',
Expand All @@ -16,13 +16,13 @@ const monthConverter = {
11: 'Dec',
};

const isLast24Hrs = (date) => {
const isLast24Hrs = (date: any) => {
const timeStamp = Math.round(new Date().getTime() / 1000);
const timeStampYesterday = timeStamp - 24 * 3600;
return date >= new Date(timeStampYesterday * 1000).getTime();
};

export const dateFormatter = (dateString) => {
export const dateFormatter = (dateString: string) => {
const date = new Date(dateString);

if (isLast24Hrs(date)) {
Expand Down
File renamed without changes.
5 changes: 3 additions & 2 deletions src/lib/useInterval.js → src/lib/useInterval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect, useRef } from 'react';

// Why do we have this custom hook?
// https://overreacted.io/making-setinterval-declarative-with-react-hooks/
export const useInterval = (callback, delay) => {
export const useInterval = (callback: any, delay: number) => {
const savedCallback = useRef();

// Remember the latest callback.
Expand All @@ -13,7 +13,8 @@ export const useInterval = (callback, delay) => {
// Set up the interval.
useEffect(() => {
function tick() {
savedCallback.current();
const currentCallback: any = savedCallback.current;
currentCallback();
}
if (delay !== null) {
let id = setInterval(tick, delay);
Expand Down
78 changes: 62 additions & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3793,7 +3793,7 @@
dependencies:
"@types/istanbul-lib-report" "*"

"@types/jest@*", "@types/jest@^26.0.0":
"@types/jest@*", "@types/jest@26.x", "@types/jest@^26.0.0":
version "26.0.20"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-26.0.20.tgz#cd2f2702ecf69e86b586e1f5223a60e454056307"
integrity sha512-9zi2Y+5USJRxd0FsahERhBwlcvFh6D2GLQnY2FH2BzK8J9s9omvNHIbvABwIluXa0fD8XVKMLTO0aOEuUfACAA==
Expand Down Expand Up @@ -5746,14 +5746,21 @@ browserslist@^4.14.5, browserslist@^4.15.0:
escalade "^3.1.1"
node-releases "^1.1.67"

[email protected]:
version "0.2.6"
resolved "https://registry.yarnpkg.com/bs-logger/-/bs-logger-0.2.6.tgz#eb7d365307a72cf974cc6cda76b68354ad336bd8"
integrity sha512-pd8DCoxmbgc7hyPKOvxtqNcjYoOsABPQdcCUjGp3d42VR2CX1ORhk2A87oqqu5R1kk+76nsxZupkmyd+MVtCog==
dependencies:
fast-json-stable-stringify "2.x"

bser@^2.0.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/bser/-/bser-2.1.1.tgz#e6787da20ece9d07998533cfd9de6f5c38f4bc05"
integrity sha512-gQxTNE/GAfIIrmHLUE3oJyp5FO6HRBfhjnw4/wMmA63ZGDJnWBmgY/lyQBpnDUkGmAhbSe39tx2d/iTOAfglwQ==
dependencies:
node-int64 "^0.4.0"

buffer-from@^1.0.0:
buffer-from@1.x, buffer-from@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==
Expand Down Expand Up @@ -8306,6 +8313,11 @@ fast-json-parse@^1.0.3:
resolved "https://registry.yarnpkg.com/fast-json-parse/-/fast-json-parse-1.0.3.tgz#43e5c61ee4efa9265633046b770fb682a7577c4d"
integrity sha512-FRWsaZRWEJ1ESVNbDWmsAlqDk96gPQezzLghafp5J4GUKjbCz3OkAHuZs5TuPEtkbVQERysLp9xv6c24fBm8Aw==

[email protected]:
version "2.1.0"
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
integrity sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==

fast-json-stable-stringify@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz#d5142c0caee6b1189f87d3a76111064f86c8bbf2"
Expand Down Expand Up @@ -11152,7 +11164,7 @@ jest-util@^25.5.0:
is-ci "^2.0.0"
make-dir "^3.0.0"

jest-util@^26.6.2:
jest-util@^26.1.0, jest-util@^26.6.2:
version "26.6.2"
resolved "https://registry.yarnpkg.com/jest-util/-/jest-util-26.6.2.tgz#907535dbe4d5a6cb4c47ac9b926f6af29576cbc1"
integrity sha512-MDW0fKfsn0OI7MS7Euz6h8HNDXVQ0gaM9uW6RjfDmd1DAFcaxX9OqIakHIqhbnmF08Cf2DLDG+ulq8YQQ0Lp0Q==
Expand Down Expand Up @@ -11484,20 +11496,20 @@ json3@^3.3.2:
resolved "https://registry.yarnpkg.com/json3/-/json3-3.3.3.tgz#7fc10e375fc5ae42c4705a5cc0aa6f62be305b81"
integrity sha512-c7/8mbUsKigAbLkD5B010BK4D9LZm7A1pNItkEwiUZRpIN66exu/e7YQWysGun+TRKaJp8MhemM+VkfWv42aCA==

[email protected], json5@^2.1.0, json5@^2.1.1, json5@^2.1.2:
version "2.1.3"
resolved "https://registry.yarnpkg.com/json5/-/json5-2.1.3.tgz#c9b0f7fa9233bfe5807fe66fcf3a5617ed597d43"
integrity sha512-KXPvOm8K9IJKFM0bmdn8QXh7udDh1g/giieX0NLCaMnb4hEiVFqnop2ImTXCc5e0/oHz3LTqmHGtExn5hfMkOA==
dependencies:
minimist "^1.2.5"

json5@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/json5/-/json5-1.0.1.tgz#779fb0018604fa854eacbf6252180d83543e3dbe"
integrity sha512-aKS4WQjPenRxiQsC93MNfjx+nbF4PAdYzmd/1JIj8HYzqfbu86beTuNgXDzPknWk0n0uARlyewZo4s++ES36Ow==
dependencies:
minimist "^1.2.0"

json5@^2.1.0, json5@^2.1.1, json5@^2.1.2:
version "2.1.3"
resolved "https://registry.yarnpkg.com/json5/-/json5-2.1.3.tgz#c9b0f7fa9233bfe5807fe66fcf3a5617ed597d43"
integrity sha512-KXPvOm8K9IJKFM0bmdn8QXh7udDh1g/giieX0NLCaMnb4hEiVFqnop2ImTXCc5e0/oHz3LTqmHGtExn5hfMkOA==
dependencies:
minimist "^1.2.5"

jsonfile@^2.1.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-2.4.0.tgz#3736a2b428b87bbda0cc83b53fa3d633a35c2ae8"
Expand Down Expand Up @@ -11771,7 +11783,7 @@ lodash.isequal@^4.5.0:
resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"
integrity sha1-QVxEePK8wwEgwizhDtMib30+GOA=

lodash.memoize@^4.1.2:
lodash.memoize@4.x, lodash.memoize@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
integrity sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=
Expand Down Expand Up @@ -11893,6 +11905,11 @@ make-dir@^3.0.2, make-dir@^3.1.0:
dependencies:
semver "^6.0.0"

[email protected]:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==

[email protected]:
version "1.0.11"
resolved "https://registry.yarnpkg.com/makeerror/-/makeerror-1.0.11.tgz#e01a5c9109f2af79660e4e8b9587790184f5a96c"
Expand Down Expand Up @@ -12341,18 +12358,18 @@ [email protected], mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.1:
dependencies:
minimist "0.0.8"

[email protected], mkdirp@^1.0.3, mkdirp@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==

mkdirp@^0.5.3:
version "0.5.5"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.5.tgz#d91cefd62d1436ca0f41620e251288d420099def"
integrity sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==
dependencies:
minimist "^1.2.5"

mkdirp@^1.0.3, mkdirp@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==

mockdate@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/mockdate/-/mockdate-3.0.2.tgz#a5a7bb5820da617747af424d7a4dcb22c6c03d79"
Expand Down Expand Up @@ -15767,6 +15784,13 @@ [email protected]:
resolved "https://registry.yarnpkg.com/semver/-/semver-7.0.0.tgz#5f3ca35761e47e05b206c6daff2cf814f0316b8e"
integrity sha512-+GB6zVA9LWh6zovYQLALHwv5rb2PHGlJi3lfiqIHxR0uuwCgefcOJc59v9fv1w8GbStwxuuqqAjI9NMAOOgq1A==

[email protected]:
version "7.3.4"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.4.tgz#27aaa7d2e4ca76452f98d3add093a72c943edc97"
integrity sha512-tCfb2WLjqFAtXn4KEdxIhalnRtoKFN7nAwj0B3ZXCbQloV2tq5eDbcTmT68JJD3nRJq24/XgxtQKFIpQdtvmVw==
dependencies:
lru-cache "^6.0.0"

semver@^7.1.3:
version "7.3.2"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.2.tgz#604962b052b81ed0786aae84389ffba70ffd3938"
Expand Down Expand Up @@ -17055,6 +17079,23 @@ ts-essentials@^2.0.3:
resolved "https://registry.yarnpkg.com/ts-essentials/-/ts-essentials-2.0.12.tgz#c9303f3d74f75fa7528c3d49b80e089ab09d8745"
integrity sha512-3IVX4nI6B5cc31/GFFE+i8ey/N2eA0CZDbo6n0yrz0zDX8ZJ8djmU1p+XRz7G3is0F3bB3pu2pAroFdAWQKU3w==

ts-jest@^26.4.4:
version "26.4.4"
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-26.4.4.tgz#61f13fb21ab400853c532270e52cc0ed7e502c49"
integrity sha512-3lFWKbLxJm34QxyVNNCgXX1u4o/RV0myvA2y2Bxm46iGIjKlaY0own9gIckbjZJPn+WaJEnfPPJ20HHGpoq4yg==
dependencies:
"@types/jest" "26.x"
bs-logger "0.x"
buffer-from "1.x"
fast-json-stable-stringify "2.x"
jest-util "^26.1.0"
json5 "2.x"
lodash.memoize "4.x"
make-error "1.x"
mkdirp "1.x"
semver "7.x"
yargs-parser "20.x"

[email protected], ts-pnp@^1.1.2:
version "1.1.5"
resolved "https://registry.yarnpkg.com/ts-pnp/-/ts-pnp-1.1.5.tgz#840e0739c89fce5f3abd9037bb091dbff16d9dec"
Expand Down Expand Up @@ -18192,6 +18233,11 @@ yaml@^1.7.2:
dependencies:
"@babel/runtime" "^7.6.3"

[email protected]:
version "20.2.4"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.4.tgz#b42890f14566796f85ae8e3a25290d205f154a54"
integrity sha512-WOkpgNhPTlE73h4VFAFsOnomJVaovO8VqLDzy5saChRBFQFBoMYirowyW+Q9HB4HFF4Z7VZTiG3iSzJJA29yRA==

yargs-parser@^11.1.1:
version "11.1.1"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-11.1.1.tgz#879a0865973bca9f6bab5cbdf3b1c67ec7d3bcf4"
Expand Down

0 comments on commit 96d919e

Please sign in to comment.