-
Notifications
You must be signed in to change notification settings - Fork 15
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
Set up testing environment #682
Changes from all commits
1cf6413
33dd3b7
d6a88a0
af00a32
e9932e4
a0475aa
e3940c2
641244a
a9724fe
84b0047
4adcbcf
7199389
ea341af
c32b939
6215f86
3e540d9
550699a
ca5f00d
f3de2f0
533707a
77e2696
8279e5a
c0b3770
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,4 +21,5 @@ dist/ | |
build/ | ||
.nyc_output/ | ||
coverage/ | ||
!.vscode/extensions.json | ||
!.vscode/extensions.json | ||
*.tsbuildinfo |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"jsc": { | ||
"parser": { | ||
"syntax": "typescript", | ||
"tsx": true | ||
}, | ||
"transform": { | ||
"react": { | ||
"runtime": "automatic" | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const detect = jest.fn().mockReturnValue( { | ||
os: 'Windows 10', | ||
} ) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module.exports = { | ||
transform: { | ||
'^.+\\.(t|j)sx?$': [ '@swc/jest', | ||
{ | ||
jsc: { | ||
parser: { | ||
syntax: 'typescript', | ||
tsx: true, | ||
}, | ||
transform: { | ||
react: { | ||
runtime: 'automatic', | ||
}, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
testEnvironment: 'jsdom', | ||
moduleNameMapper: { | ||
'\\.css$': '<rootDir>/src/__mocks__/styleMock.ts', | ||
}, | ||
setupFilesAfterEnv: [ | ||
'<rootDir>/tests/setupTests.ts', | ||
], | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { render, screen } from '@testing-library/react' | ||
|
||
import About from './About' | ||
|
||
jest.mock( '../lib/controller', jest.fn() ) | ||
|
||
describe( '<About />', () => { | ||
it( 'should display a loading spinner when the page is loading', async () => { | ||
render( <About connected={0} /> ) | ||
|
||
const loadingSpinner = await screen.findByRole( 'progressbar' ) | ||
expect( loadingSpinner ).toBeInTheDocument() | ||
} ) | ||
|
||
it.todo( 'should show the number of connected devices' ) | ||
} ) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = {} | ||
|
||
export default {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const isMac = false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import copy from 'copy-to-clipboard' | ||
import { useSnackbar } from 'notistack' | ||
|
||
import { useCopyToClipboard } from './hooks' | ||
|
||
jest.mock( 'copy-to-clipboard', () => jest.fn() ) | ||
jest.mock( 'notistack', () => ( { useSnackbar: jest.fn().mockReturnValue( { enqueueSnackbar: jest.fn() } ) } ) ) | ||
|
||
const TestHooksComponent = () => { | ||
const copyToClipboard = useCopyToClipboard() | ||
|
||
return { copyToClipboard } | ||
} | ||
|
||
describe( 'hooks', () => { | ||
describe( 'useCopyToClipboard()', () => { | ||
let copyToClipboard: ( text: string, fallback?: string ) => void | ||
let enqueueSnackbar | ||
|
||
beforeAll( () => { | ||
( { copyToClipboard } = TestHooksComponent() ) | ||
;( { enqueueSnackbar } = useSnackbar() ) | ||
} ) | ||
|
||
afterEach( () => { | ||
jest.clearAllMocks() | ||
} ) | ||
|
||
it( 'should return a function to copy the given text to the clipboard', () => { | ||
copyToClipboard( 'text to copy to the clipboard' ) | ||
|
||
expect( copy ).toHaveBeenCalledTimes( 2 ) | ||
expect( copy ).toHaveBeenCalledWith( 'text to copy to the clipboard' ) | ||
} ) | ||
|
||
it( 'should truncate the snackbar message to a max of 30 characters', () => { | ||
copyToClipboard( 'this is 30 characters longggg. this should be truncated.' ) | ||
|
||
// eslint-disable-next-line no-useless-escape | ||
expect( enqueueSnackbar ).toHaveBeenCalledWith( 'Copied \"this is 30 characters longggg....\" to clipboard', expect.any( Object ) ) | ||
} ) | ||
|
||
it( 'should return the fallback text in the snackbar if a falsy value is passed', () => { | ||
const { enqueueSnackbar } = useSnackbar() | ||
copyToClipboard( '', 'fallback text' ) | ||
|
||
expect( copy ).not.toHaveBeenCalled() | ||
expect( enqueueSnackbar ).toHaveBeenCalledWith( 'fallback text', expect.any( Object ) ) | ||
} ) | ||
} ) | ||
} ) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { mapPlatformKey, mapPlatformKeys } from './utils' | ||
|
||
const isMacMockGetter = jest.fn() | ||
|
||
jest.mock( './consts', () => ( { | ||
get isMac() { | ||
return isMacMockGetter() | ||
}, | ||
} ) ) | ||
Comment on lines
+3
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very interesting approach! I think there might be a simpler option:
might work There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
describe( 'utils', () => { | ||
describe( 'mapPlatformKey', () => { | ||
it( 'should map ctrl to cmd if on Mac', () => { | ||
isMacMockGetter.mockReturnValue( true ) | ||
|
||
expect( mapPlatformKey( 'alt+V' ) ).toBe( 'alt+V' ) | ||
expect( mapPlatformKey( 'ctrl+V' ) ).toBe( 'cmd+V' ) | ||
} ) | ||
|
||
it( 'should keep the key as-is if not on Mac', () => { | ||
isMacMockGetter.mockReturnValue( false ) | ||
|
||
expect( mapPlatformKey( '' ) ).toBe( '' ) | ||
expect( mapPlatformKey( 'ctrl+D' ) ).toBe( 'ctrl+D' ) | ||
} ) | ||
} ) | ||
|
||
describe( 'mapPlatformKeys', () => { | ||
it( 'should not fail if passed an empty object', () => { | ||
const result = mapPlatformKeys( {} ) | ||
|
||
expect( result ).toEqual( {} ) | ||
} ) | ||
|
||
it( 'should return the keyMap argument as-is if not on Mac', () => { | ||
isMacMockGetter.mockReturnValue( false ) | ||
mapPlatformKeys( {} ) | ||
} ) | ||
|
||
it.todo( 'should transform ctrl to cmd in key bindings if on Mac' ) | ||
} ) | ||
} ) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { render } from '@testing-library/react' | ||
|
||
import { StatusContext } from '../lib/contexts' | ||
import ThemeLoader from './ThemeLoader' | ||
|
||
describe( 'ThemeLoader', () => { | ||
it( 'should generate a link element with a href of the default Day.css value', () => { | ||
const { container } = render( | ||
<StatusContext.Provider | ||
value={{ connected: false, connectedAt: null, status: null }} | ||
> | ||
<ThemeLoader /> | ||
</StatusContext.Provider> | ||
) | ||
|
||
const link = container.querySelector( 'link' ) | ||
|
||
expect( link ).toBeInTheDocument() | ||
expect( link?.getAttribute( 'href' ) ).toMatch( /Day.css/ ) | ||
} ) | ||
|
||
it( | ||
'should generate a link element with a href of the given name value', | ||
() => { | ||
const { container } = render( | ||
<StatusContext.Provider | ||
value={{ connected: false, connectedAt: null, status: null }} | ||
> | ||
<ThemeLoader name="Night" /> | ||
</StatusContext.Provider> | ||
) | ||
|
||
const link = container.querySelector( 'link' ) | ||
|
||
expect( link ).toBeInTheDocument() | ||
expect( link?.getAttribute( 'href' ) ).toMatch( /Night.css/ ) | ||
} | ||
) | ||
} ) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import '@testing-library/jest-dom' | ||
|
||
jest.mock( 'detect-browser' ) | ||
|
||
global.fetch = jest.fn().mockResolvedValue( { | ||
json: () => Promise.resolve( {} ), | ||
} ) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,19 @@ | ||
/// jest.d.ts | ||
|
||
module.exports = { | ||
projects: [ '<rootDir>/apps/*', '<rootDir>/packages/*' ], | ||
transform: { | ||
'^.+\\.(t|j)sx?$': [ '@swc/jest' ], | ||
'^.+\\.(t|j)sx?$': '@swc/jest', | ||
}, | ||
testEnvironment: 'jsdom', | ||
moduleNameMapper: { | ||
'\\.css$': '<rootDir>/apps/frontend/src/__mocks__/styleMock.ts', | ||
}, | ||
resetMocks: true, | ||
// resetMocks: true, | ||
// Indicates whether the coverage information should be collected while executing the test | ||
collectCoverage: true, | ||
|
||
// The directory where Jest should output its coverage files | ||
coverageDirectory: 'coverage', | ||
|
||
// Indicates which provider should be used to instrument code for coverage | ||
coverageProvider: 'v8', | ||
Comment on lines
+15
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these options required? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These were created from the initial Jest setup, I think. Do you want me to remove them? |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This suite ends up testing a lot of the internals of the function, rather than outcome! You can try to test the outcome of hooks by setting up a scaffolding React component, and then asserting against the outcomes, as we'd normally do :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So with this, I'm looking to implement:
Does that sound sensible?