-
Notifications
You must be signed in to change notification settings - Fork 6
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
Setup Testing #92
Merged
Merged
Setup Testing #92
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fcd08a2
test: kickoff tests for Counter
felipecadavid 82652d7
fix: missing types
felipecadavid 3ea86c8
test: kickoff TimeSeries testing
felipecadavid b8e7468
chore: yarn.lock
felipecadavid 59afb0f
test: kickoff Leaderboard testing
felipecadavid f63bffb
test: set mockData for timeseries
felipecadavid 185d76f
fix: import order
felipecadavid c16809f
test: set mockData for Counter
felipecadavid b72ae67
refactor: getByRole instead of test id
felipecadavid f40968f
test: add additional query response tests for counter
felipecadavid e0632ca
fix: remove unnecessary console.log
felipecadavid a85a576
chore: run tests with turbo instead of yarn workspaces
felipecadavid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nice 👍 Since we are here, how about expanding this to show
null
due to lack of data (this will happen for AVERAGE, MIN, MAX, etc.).null
due to error (this could happen due to timeout, permissions, etc.).And going ahead and getting the tests for the second two into the <Counter> component's tests? I'm assuming they will just pass but, if they don't we can skip them and address in a followup.
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.
Yes! I just added tests for those