Skip to content

Commit 7d2cf17

Browse files
authored
Merge pull request #962 from weaveworks/jp-fe-tests
Add example front-end tests
2 parents 56f1161 + 32905ec commit 7d2cf17

File tree

14 files changed

+2108
-34
lines changed

14 files changed

+2108
-34
lines changed

.github/workflows/test.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,31 @@ jobs:
154154
cd cmd/clusters-service
155155
go test -v ./... -tags=integration
156156
157+
ui-unit-tests:
158+
runs-on: ubuntu-latest
159+
env:
160+
GITHUB_TOKEN: ${{ secrets.BUILD_BOT_PERSONAL_ACCESS_TOKEN }}
161+
YARN_CACHE_NAME: cache-yarn
162+
steps:
163+
- id: cache-paths
164+
run: |
165+
echo "::set-output name=dir::$(yarn cache dir)"
166+
echo "::set-output name=go-build::$(go env GOCACHE)"
167+
echo "::set-output name=go-mod::$(go env GOMODCACHE)"
168+
- name: Checkout code
169+
uses: actions/checkout@v3
170+
- name: Yarn Cache
171+
uses: actions/cache@v2
172+
with:
173+
path: ${{ steps.cache-paths.outputs.dir }}
174+
key: ${{ runner.os }}-${{ env.YARN_CACHE_NAME }}-${{ hashFiles('**/yarn.lock') }}
175+
restore-keys: |
176+
${{ runner.os }}-${{ env.YARN_CACHE_NAME }}-
177+
- name: Install modules
178+
run: cd ui-cra && yarn --pure-lockfile
179+
- name: Run Front-end Unit Tests
180+
run: cd ui-cra && ./node_modules/.bin/react-scripts test
181+
157182
ui-integration-tests:
158183
runs-on: ubuntu-latest
159184
env:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
tools/bin
1717

1818
node_modules
19+
tilt_modules
1920

2021
# Ignore binary dependencies
2122
/setup/wk-quickstart/bin/

CONTRIBUTING.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,50 @@ yarn start
419419
420420
Open up http://localhost:3000. Changes to code will be hot-reloaded.
421421
422+
### Unit Tests
423+
424+
To start the `jest` test runner CLI dialog:
425+
426+
```
427+
$ cd ui-cra
428+
$ yarn test
429+
430+
PASS src/components/Applications/__tests__/index.test.tsx
431+
Applications index test
432+
✓ renders table rows (349 ms)
433+
snapshots
434+
✓ loading (170 ms)
435+
✓ success (168 ms)
436+
437+
Test Suites: 1 passed, 1 total
438+
Tests: 3 passed, 3 total
439+
Snapshots: 2 passed, 2 total
440+
Time: 5.448 s
441+
Ran all test suites.
442+
443+
Watch Usage: Press w to show more.
444+
```
445+
446+
#### UI Unit Test Tips
447+
448+
- The `@testing-library/react` package provides a test renderer as well as helpers for dealing with hooks and component state
449+
- Snapshots alone generally aren't enough, you should do some assertions to validate component behavior
450+
- Hooks can be tested in isolation from components using the `act` helper.
451+
452+
#### Snapshot Tests
453+
454+
We use a technique called "Snapshots" to record the rendered output of components and track them in version control over time. Snapshots are not really tests, since they don't have any explicity assertions. Think of them more as a record of the output of a component.
455+
456+
When combined with the `styled-components` integration, snapshots give us a way to track styling logic over time. This can be very helpful in debugging styling issues that would otherwise be hard to understand.
457+
458+
After any changes to styling logic, you should expect to update snapshots, else unit tests will fail.
459+
460+
To update snapshots:
461+
462+
```
463+
yarn test -u
464+
```
465+
422466
### How to do local dev on the UI
423467
424468
The easiest way to dev on the UI is to use an existing cluster.

ui-cra/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"@types/react-router-dom": "^5.1.7",
2626
"@types/react-syntax-highlighter": "^13.5.2",
2727
"@types/styled-components": "^5.1.9",
28-
"@weaveworks/progressive-delivery": "^0.0.0-rc8",
28+
"@weaveworks/progressive-delivery": "0.0.0-rc8",
2929
"@weaveworks/weave-gitops": "0.9.0-rc.3",
3030
"classnames": "^2.3.1",
3131
"d3-scale": "4.0.0",
@@ -95,9 +95,11 @@
9595
]
9696
},
9797
"devDependencies": {
98+
"@testing-library/react": "^12.1.5",
9899
"@types/react-syntax-highlighter": "^13.5.2",
99100
"express": "^4.17.1",
100101
"http-proxy-middleware": "^2.0.0",
102+
"jest-styled-components": "^7.0.8",
101103
"prettier": "^2.3.0"
102104
}
103105
}

ui-cra/src/App.tsx

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
import React, { FC } from 'react';
2-
import { BrowserRouter } from 'react-router-dom';
3-
import { QueryClient, QueryClientProvider } from 'react-query';
4-
import { muiTheme } from './muiTheme';
5-
import { MuiThemeProvider } from '@material-ui/core/styles';
61
import '@fortawesome/fontawesome-free/css/all.css';
2+
import { MuiThemeProvider } from '@material-ui/core/styles';
3+
import { FC } from 'react';
4+
import { QueryClient, QueryClientProvider } from 'react-query';
5+
import { BrowserRouter } from 'react-router-dom';
76
import { createGlobalStyle, ThemeProvider } from 'styled-components';
7+
import { muiTheme } from './muiTheme';
88

99
import {
10-
theme,
11-
applicationsClient,
1210
AppContextProvider,
11+
applicationsClient,
12+
theme,
1313
} from '@weaveworks/weave-gitops';
14-
import ProximaNova from './fonts/proximanova-regular.woff';
15-
import RobotoMono from './fonts/roboto-mono-regular.woff';
1614
import Background from './assets/img/background.svg';
1715
import ResponsiveDrawer from './components/ResponsiveDrawer';
16+
import RequestContextProvider from './contexts/Request';
17+
import ProximaNova from './fonts/proximanova-regular.woff';
18+
import RobotoMono from './fonts/roboto-mono-regular.woff';
1819

1920
const GlobalStyle = createGlobalStyle`
2021
/* https://github.com/weaveworks/wkp-ui/pull/283#discussion_r339958886 */
@@ -65,14 +66,16 @@ const App: FC = () => {
6566
return (
6667
<ThemeProvider theme={theme}>
6768
<MuiThemeProvider theme={muiTheme}>
68-
<QueryClientProvider client={queryClient}>
69-
<BrowserRouter basename={process.env.PUBLIC_URL}>
70-
<GlobalStyle />
71-
<AppContextProvider applicationsClient={applicationsClient}>
72-
<ResponsiveDrawer />
73-
</AppContextProvider>
74-
</BrowserRouter>
75-
</QueryClientProvider>
69+
<RequestContextProvider fetch={window.fetch}>
70+
<QueryClientProvider client={queryClient}>
71+
<BrowserRouter basename={process.env.PUBLIC_URL}>
72+
<GlobalStyle />
73+
<AppContextProvider applicationsClient={applicationsClient}>
74+
<ResponsiveDrawer />
75+
</AppContextProvider>
76+
</BrowserRouter>
77+
</QueryClientProvider>
78+
</RequestContextProvider>
7679
</MuiThemeProvider>
7780
</ThemeProvider>
7881
);

0 commit comments

Comments
 (0)