-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test: kickoff tests for Counter * fix: missing types * test: kickoff TimeSeries testing * chore: yarn.lock * test: kickoff Leaderboard testing * test: set mockData for timeseries * fix: import order * test: set mockData for Counter * refactor: getByRole instead of test id * test: add additional query response tests for counter * fix: remove unnecessary console.log * chore: run tests with turbo instead of yarn workspaces
- Loading branch information
1 parent
6504d53
commit 9c239bb
Showing
38 changed files
with
1,610 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict' | ||
|
||
const { TextEncoder, TextDecoder } = require('util') | ||
const OriginalJSDOMEnvironment = require('jest-environment-jsdom') | ||
|
||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}) | ||
|
||
class JSDOMEnvironment extends OriginalJSDOMEnvironment { | ||
constructor (...args) { | ||
const { global } = super(...args) | ||
if (!global.TextEncoder) global.TextEncoder = TextEncoder | ||
if (!global.TextDecoder) global.TextDecoder = TextDecoder | ||
} | ||
} | ||
|
||
exports.default = JSDOMEnvironment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* For a detailed explanation regarding each configuration property, visit: | ||
* https://jestjs.io/docs/configuration | ||
*/ | ||
|
||
/** @type {import('jest').Config} */ | ||
const config = { | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'json', 'jsx'], | ||
rootDir: '.', | ||
roots: ['<rootDir>'], | ||
moduleNameMapper: { | ||
'@/testing/(.*)': '<rootDir>/testing/$1', | ||
'@/testing': '<rootDir>/testing', | ||
'@/counter/(.*)': '<rootDir>/src/(.*)', | ||
'@/counter': '<rootDir>/src' | ||
}, | ||
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'], | ||
testEnvironment: './jest-environment-jsdom.cjs', | ||
transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(ts|tsx)$'], | ||
transform: { | ||
'^.+\\.(js|ts|tsx)$': [ | ||
'@swc/jest', | ||
{ | ||
jsc: { | ||
transform: { | ||
react: { | ||
runtime: 'automatic' | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
|
||
module.exports = config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Optional: configure or set up a testing framework before each test. | ||
// If you delete this file, remove `setupFilesAfterEnv` from `jest.config.js` | ||
|
||
// Used for __tests__/testing-library.js | ||
// Learn more: https://github.com/testing-library/jest-dom | ||
import '@testing-library/jest-dom/extend-expect' | ||
|
||
import { server } from './testing/mocks/server' | ||
|
||
process.env.NEXT_PUBLIC_ACCOUNT_MANAGEMENT_GRAPHQL_ENDPOINT ??= 'http://localhost:3000/in/graphql' | ||
process.env.NEXT_PUBLIC_CONFIG_MANAGEMENT_GRAPHQL_ENDPOINT ??= 'http://localhost:3000/graphql' | ||
|
||
// Establish API mocking before all tests. | ||
beforeAll(() => server.listen()) | ||
|
||
// Reset any request handlers that we may add during the tests, | ||
// so they don't affect other tests. | ||
afterEach(() => server.resetHandlers()) | ||
|
||
// Clean up after the tests are finished. | ||
afterAll(() => server.close()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import React from 'react' | ||
import { render, waitFor } from '@testing-library/react' | ||
|
||
import { Counter, RelativeTimeRange } from '@/counter' | ||
import { Dom } from '@/testing' | ||
|
||
import { setupHandlers } from './mswHandlers' | ||
import { counter } from './mockData' | ||
|
||
describe('Counter', () => { | ||
let dom: Dom | ||
|
||
beforeEach(() => { | ||
setupHandlers() | ||
}) | ||
|
||
it('should render a static counter', async () => { | ||
dom = render(<Counter value={counter.value} />) | ||
|
||
await dom.findByText(counter.value) | ||
}) | ||
|
||
it('should show value from server', async () => { | ||
dom = render( | ||
<Counter | ||
query={{ | ||
metric: 'test-metric', | ||
accessToken: 'test-token', | ||
timeRange: { relative: RelativeTimeRange.LastNDays, n: 30 } | ||
}} | ||
/> | ||
) | ||
|
||
await dom.findByText(counter.value) | ||
}) | ||
|
||
it('Should show "-" when value is null', async () => { | ||
dom = render( | ||
<Counter | ||
query={{ | ||
metric: 'lack-of-data', | ||
accessToken: 'test-token', | ||
timeRange: { relative: RelativeTimeRange.LastNDays, n: 30 } | ||
}} | ||
/> | ||
) | ||
|
||
await waitFor(async () => { | ||
await dom.findByText('-') | ||
}) | ||
}) | ||
|
||
it('Should show error fallback when request fails', async () => { | ||
dom = render( | ||
<Counter | ||
query={{ | ||
metric: 'should-fail', | ||
accessToken: 'test-token', | ||
timeRange: { relative: RelativeTimeRange.LastNDays, n: 30 } | ||
}} | ||
/> | ||
) | ||
|
||
await waitFor(async () => { | ||
await dom.findByRole('img') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const counter = { | ||
value: '123' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { graphql } from 'msw' | ||
|
||
import { server } from '@/testing/mocks/server' | ||
|
||
import { counter } from './mockData' | ||
|
||
const handlers = [ | ||
graphql.query('Counter', (req, res, ctx) => { | ||
const { metricName } = req.variables.counterInput | ||
|
||
if (metricName === 'lack-of-data') { | ||
return res( | ||
ctx.data({ | ||
counter: { | ||
value: null | ||
} | ||
}) | ||
) | ||
} | ||
|
||
if (metricName === 'should-fail') { | ||
return res( | ||
ctx.errors([ | ||
{ | ||
message: 'something went wrong' | ||
} | ||
]) | ||
) | ||
} | ||
|
||
return res( | ||
ctx.data({ | ||
counter | ||
}) | ||
) | ||
}) | ||
] | ||
|
||
export const setupHandlers = () => { | ||
server.use(...handlers) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { render } from '@testing-library/react' | ||
|
||
/** | ||
* This is a convenience type for referring to the result of calling `render`. | ||
*/ | ||
export type Dom = ReturnType<typeof render> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { setupServer } from 'msw/node' | ||
|
||
export const server = setupServer() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"jsx": "preserve", | ||
"target": "es2016", | ||
"moduleResolution": "node", | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"strict": true, | ||
"skipLibCheck": true, | ||
"paths": { | ||
"@/testing/*": ["testing/*"], | ||
"@/testing": ["testing"], | ||
"@/counter": ["src"] | ||
} | ||
}, | ||
"include": ["**/*.ts", "**/*.tsx", "./jest.config.js", "./jest.setup.js"], | ||
"exclude": ["node_modules"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict' | ||
|
||
const { TextEncoder, TextDecoder } = require('util') | ||
const OriginalJSDOMEnvironment = require('jest-environment-jsdom') | ||
|
||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}) | ||
|
||
class JSDOMEnvironment extends OriginalJSDOMEnvironment { | ||
constructor (...args) { | ||
const { global } = super(...args) | ||
if (!global.TextEncoder) global.TextEncoder = TextEncoder | ||
if (!global.TextDecoder) global.TextDecoder = TextDecoder | ||
} | ||
} | ||
|
||
exports.default = JSDOMEnvironment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* For a detailed explanation regarding each configuration property, visit: | ||
* https://jestjs.io/docs/configuration | ||
*/ | ||
|
||
/** @type {import('jest').Config} */ | ||
const config = { | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'json', 'jsx'], | ||
rootDir: '.', | ||
roots: ['<rootDir>'], | ||
moduleNameMapper: { | ||
'@/testing/(.*)': '<rootDir>/testing/$1', | ||
'@/testing': '<rootDir>/testing', | ||
'@/leaderboard/(.*)': '<rootDir>/src/(.*)', | ||
'@/leaderboard': '<rootDir>/src' | ||
}, | ||
setupFilesAfterEnv: ['<rootDir>/jest.setup.js', 'jest-canvas-mock'], | ||
testEnvironment: './jest-environment-jsdom.cjs', | ||
transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(ts|tsx)$'], | ||
transform: { | ||
'^.+\\.(js|ts|tsx)$': [ | ||
'@swc/jest', | ||
{ | ||
jsc: { | ||
transform: { | ||
react: { | ||
runtime: 'automatic' | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
|
||
module.exports = config |
Oops, something went wrong.