Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove withRouter (1/n) #3007

Merged
merged 25 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d9f468b
Functionalize RandomKawaii
taneliang Nov 10, 2020
44cd5ce
Functionalize KeyboardShortcuts
taneliang Nov 10, 2020
e125590
Functionalize ScrollToTop
taneliang Nov 10, 2020
93bcb64
Functionalize AppShell
taneliang Nov 30, 2020
6573396
Functionalize LessonTimetable
taneliang Nov 30, 2020
f5ccc16
Functionalize ModRegNotification
taneliang Nov 30, 2020
4559d71
Add React Testing Library
taneliang Nov 30, 2020
6b38102
Rewrite GlobalSearchContainer tests with RTL
taneliang Nov 30, 2020
34c8bff
Introduce __TEST__ global var
taneliang Dec 1, 2020
8846526
Test GlobalSearchContainer wrapped in withRouter and connect HOCs
taneliang Dec 1, 2020
87ec3ac
Functionalize GlobalSearchContainer
taneliang Dec 1, 2020
08d7e1f
Define new useMediaQuery hook to replace makeResponsive HOC
taneliang Dec 1, 2020
2008c60
Make mockDom mock more robust; mock matchMedia in GlobalSearchContain…
taneliang Dec 1, 2020
aea48bb
Replace makeResponsive -> useMediaQuery in GlobalSearchContainer
taneliang Dec 1, 2020
19e9e08
Fix LessonTimetable type error
taneliang Dec 1, 2020
b31bc83
Add @testing-library/* to Renovate test group
taneliang Dec 2, 2020
10e4f49
Fix nits in css.ts
taneliang Dec 2, 2020
d925401
Migrate ScrollToTop from enzyme -> RTL
taneliang Dec 2, 2020
6b84a2c
Fix nits
taneliang Dec 2, 2020
b675711
Split GlobalSearchContainer show many results test
taneliang Dec 2, 2020
02e91a6
Fix review comments
taneliang Dec 2, 2020
fb67e1f
Define new, simpler useScrollToTopEffect hook to replace ScrollToTop
taneliang Dec 2, 2020
2e4f29a
Nit: clarify useScrollToTopEffect useEffect comment
taneliang Dec 2, 2020
8b0cf98
Nit: further clarify useScrollToTopEffect comments
taneliang Dec 2, 2020
f019ae1
Fix review comments
taneliang Dec 3, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
},
{
"extends": "monorepo:jest",
"packagePatterns": ["(jest|enzyme|mock)"],
"packagePatterns": ["(jest|enzyme|mock)", "^@testing-library/*"],
"packageNames": ["identity-obj-proxy"],
"groupName": "test"
},
Expand Down
5 changes: 3 additions & 2 deletions website/jest.common.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
roots: ['<rootDir>/src'],
moduleDirectories: ['node_modules', '<rootDir>/src'],
moduleFileExtensions: ['jsx', 'js', 'ts', 'tsx'],
setupFiles: ['<rootDir>/scripts/test.js'],
setupFilesAfterEnv: ['<rootDir>/scripts/test.js'],
moduleNameMapper: {
// Mock non JS files as strings
'\\.(?:jpg|jpeg|png|gif|eot|otf|webp|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
Expand All @@ -16,7 +16,8 @@ module.exports = {
// Mimic the globals we set with Webpack's DefinePlugin
globals: {
// Default to development
__DEV__: process.env.NODE_ENV !== 'production',
__DEV__: process.env.NODE_ENV === 'development',
__TEST__: process.env.NODE_ENV === 'test',
DATA_API_BASE_URL: '',
VERSION_STR: '',
DISPLAY_COMMIT_HASH: '',
Expand Down
7 changes: 6 additions & 1 deletion website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
"@packtracker/webpack-plugin": "2.3.0",
"@pmmmwh/react-refresh-webpack-plugin": "0.4.3",
"@svgr/webpack": "5.5.0",
"@testing-library/jest-dom": "5.11.6",
"@testing-library/react": "11.2.2",
"@testing-library/user-event": "12.3.0",
"@types/classnames": "2.2.11",
"@types/enzyme": "3.10.8",
"@types/jest": "26.0.15",
Expand All @@ -57,6 +60,7 @@
"@types/react-router-dom": "5.1.6",
"@types/react-scrollspy": "3.3.3",
"@types/redux-mock-store": "1.0.2",
"@types/use-subscription": "1.0.0",
"@types/webpack-env": "1.16.0",
"@typescript-eslint/eslint-plugin": "4.8.2",
"@typescript-eslint/parser": "4.8.2",
Expand Down Expand Up @@ -165,7 +169,8 @@
"redux-persist": "6.0.0",
"redux-thunk": "2.3.0",
"reselect": "4.0.0",
"searchkit": "2.4.1-alpha.5"
"searchkit": "2.4.1-alpha.5",
"use-subscription": "1.5.1"
},
"browserslist": [
"extends browserslist-config-nusmods"
Expand Down
1 change: 1 addition & 0 deletions website/scripts/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { configure } = require('enzyme');
const { setAutoFreeze } = require('immer');
const Adapter = require('@wojtekmaj/enzyme-adapter-react-17');
require('@testing-library/jest-dom');

configure({ adapter: new Adapter() });

Expand Down
1 change: 1 addition & 0 deletions website/src/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ module.exports = {
// Mimic the globals we set with Webpack's DefinePlugin
globals: {
__DEV__: 'readonly',
__TEST__: 'readonly',
DATA_API_BASE_URL: 'readonly',
VERSION_STR: 'readonly',
DISPLAY_COMMIT_HASH: 'readonly',
Expand Down
4 changes: 2 additions & 2 deletions website/src/actions/venueBank.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { requestAction } from 'actions/requests';
import NUSModsApi from 'apis/nusmods';
import config from 'config';
import { RequestActions } from 'middlewares/requests-middleware';
import { VenueList } from 'types/venues';
import type { RequestActions } from 'middlewares/requests-middleware';
import type { VenueList } from 'types/venues';

export const FETCH_VENUE_LIST = 'FETCH_VENUE_LIST';
export function fetchVenueList() {
Expand Down
8 changes: 4 additions & 4 deletions website/src/bootstrapping/configure-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import requestsMiddleware from 'middlewares/requests-middleware';
import ravenMiddleware from 'middlewares/raven-middleware';
import getLocalStorage from 'storage/localStorage';

import { GetState } from 'types/redux';
import { State } from 'types/state';
import { Actions } from 'types/actions';
import type { GetState } from 'types/redux';
import type { State } from 'types/state';
import type { Actions } from 'types/actions';

// For redux-devtools-extensions - see
// https://github.com/zalmoxisus/redux-devtools-extension
Expand All @@ -36,7 +36,7 @@ export default function configureStore(defaultState?: State) {
duration: true,
diff: true,
// Avoid diffing actions that insert a lot of stuff into the state to prevent console from lagging
diffPredicate: (getState: GetState, action: Actions) =>
diffPredicate: (_getState: GetState, action: Actions) =>
!action.type.startsWith('FETCH_MODULE_LIST') && !action.type.startsWith('persist/'),
});
middlewares.push(logger);
Expand Down
2 changes: 1 addition & 1 deletion website/src/bootstrapping/sentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as Sentry from '@sentry/browser';
import { isBrowserSupported } from './browser';

// Configure Raven - the client for Sentry, which we use to handle errors
const loadRaven = !__DEV__;
const loadRaven = !__DEV__ && !__TEST__;
if (loadRaven) {
Sentry.init({
dsn: 'https://[email protected]/213986',
Expand Down
7 changes: 5 additions & 2 deletions website/src/entry/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ ReactModal.setAppElement('#app');
ReactDOM.render(<App store={store} persistor={persistor} />, document.getElementById('app'));

if (
(!__DEV__ && 'serviceWorker' in navigator && window.location.protocol === 'https:') ||
(!__DEV__ &&
!__TEST__ &&
'serviceWorker' in navigator &&
window.location.protocol === 'https:') ||
// Allow us to force service worker to be enabled for debugging
DEBUG_SERVICE_WORKER
) {
registerServiceWorker(store);
}

if (!__DEV__) {
if (!__DEV__ && !__TEST__) {
initializeMamoto();
}
38 changes: 35 additions & 3 deletions website/src/test-utils/mockDom.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,49 @@
export default function mockDom() {
const nativeScrollTo = window.scrollTo;
const nativePerformance = window.performance;
const nativeMatchMedia = window.matchMedia;
const nativeScrollIntoView = Element.prototype.scrollIntoView;

export function mockDom() {
// Mock some of the DOM environment functions that are missing from JSDom
window.scrollTo = jest.fn();

if (!window.performance) {
(window as any).performance = { now: jest.fn() };
// @ts-expect-error We insist
window.performance = { now: jest.fn() };
}

if (!window.matchMedia) {
(window as any).matchMedia = jest.fn(() => ({ matches: jest.fn(), addListener: jest.fn() }));
mockWindowMatchMedia();
}

// JSDom does not stub scrollIntoView - https://github.com/jsdom/jsdom/issues/1695
if (!Element.prototype.scrollIntoView) {
Element.prototype.scrollIntoView = jest.fn();
}
}

export function mockDomReset() {
window.scrollTo = nativeScrollTo;

// @ts-expect-error We insist
window.performance = nativePerformance;

window.matchMedia = nativeMatchMedia;

Element.prototype.scrollIntoView = nativeScrollIntoView;
}

export function mockWindowMatchMedia(overrides: Partial<typeof window.matchMedia> = {}) {
// Source: https://jestjs.io/docs/en/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
window.matchMedia = jest.fn((query) => ({
matches: true,
media: query,
onchange: null,
addListener: jest.fn(), // deprecated
removeListener: jest.fn(), // deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
...overrides,
}));
}
4 changes: 3 additions & 1 deletion website/src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Globals injected by Webpack DefinePlugin
// eslint-disable-next-line no-underscore-dangle
/* eslint-disable no-underscore-dangle */
declare const __DEV__: boolean;
declare const __TEST__: boolean;
declare const DATA_API_BASE_URL: string | undefined;
declare const VERSION_STR: string | undefined;
declare const DISPLAY_COMMIT_HASH: string | undefined;
declare const DEBUG_SERVICE_WORKER: boolean;
/* eslint-enable no-underscore-dangle */

/**
* The declarations below let us use Webpack loaders to load non-JS files
Expand Down
23 changes: 9 additions & 14 deletions website/src/utils/css.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
// Define media breakpoints
import json2mq, { QueryObject } from 'json2mq';
import { entries } from 'lodash';
import type { QueryObject } from 'json2mq';

export type Breakpoint = 'xs' | 'sm' | 'md' | 'lg' | 'xl';

const breakpoints: { [breakpoint: string]: number } = {
// NOTE: Keep in sync with Bootstrap's breakpoints.
// Breakpoints at time of writing: https://getbootstrap.com/docs/4.5/layout/overview/
const breakpoints = Object.freeze({
taneliang marked this conversation as resolved.
Show resolved Hide resolved
xs: 0,
sm: 576,
md: 768,
lg: 992,
xl: 1200,
};
});
type Breakpoint = keyof typeof breakpoints;

function nextBreakpoint(size: Breakpoint): number | null | undefined {
const breakpointEntries = entries(breakpoints);
function nextBreakpoint(size: Breakpoint): number | null {
taneliang marked this conversation as resolved.
Show resolved Hide resolved
const breakpointEntries = Object.entries(breakpoints);
const nextBreakpointIndex =
breakpointEntries.findIndex(([breakpoint]) => breakpoint === size) + 1;
if (nextBreakpointIndex >= breakpointEntries.length) return null;
Expand All @@ -22,7 +21,7 @@ function nextBreakpoint(size: Breakpoint): number | null | undefined {

export function breakpointDown(size: Breakpoint): QueryObject {
const nextSize = nextBreakpoint(size);
if (nextSize == null) return { all: true };
if (nextSize === null) return { all: true };
return { maxWidth: nextSize - 1 };
}

Expand All @@ -34,10 +33,6 @@ export function touchScreenOnly(): QueryObject {
return { pointer: 'coarse' };
}

export function queryMatch(query: QueryObject) {
return window.matchMedia(json2mq(query));
}
Comment on lines -37 to -39
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead code originally used in module search before we moved to Elasticsearch


export function supportsCSSVariables() {
// Safari does not support supports('--var', 'red')
return CSS.supports && CSS.supports('(--var: red)');
Expand Down
Loading