-
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.
Merge pull request #359 from systemli/add-first-tests
✅ Add first tests
- Loading branch information
Showing
8 changed files
with
2,025 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// jest-dom adds custom jest matchers for asserting on DOM nodes. | ||
// allows you to do things like: | ||
// expect(element).toHaveTextContent(/react/i) | ||
// learn more: https://github.com/testing-library/jest-dom | ||
import '@testing-library/jest-dom' | ||
import fetchMock from 'jest-fetch-mock' | ||
|
||
fetchMock.enableMocks() | ||
|
||
process.env.REACT_APP_API_URL = 'http://localhost:8080/v1' |
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 @@ | ||
import type { Config } from '@jest/types' | ||
|
||
const config: Config.InitialOptions = { | ||
verbose: false, | ||
testEnvironment: 'jsdom', | ||
preset: 'ts-jest', | ||
moduleNameMapper: { | ||
'react-markdown': '<rootDir>/src/__mocks_/react-markdown.js', | ||
'^.+.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$': | ||
'jest-transform-stub', | ||
}, | ||
transform: { | ||
'^.+\\.(ts|tsx)$': 'ts-jest', | ||
'^.+\\.(js|jsx)$': 'babel-jest', | ||
'.+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$': | ||
'jest-transform-stub', | ||
}, | ||
transformIgnorePatterns: ['<rootDir>/node_modules/(?!react-markdown/)'], | ||
setupFilesAfterEnv: ['./jest-setup.ts'], | ||
} | ||
export default 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
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,42 @@ | ||
import { login } from './Auth' | ||
|
||
describe('Auth', function () { | ||
beforeEach(() => { | ||
fetchMock.resetMocks() | ||
fetchMock.doMock() | ||
}) | ||
|
||
test('login failed', function () { | ||
fetchMock.mockResponse( | ||
JSON.stringify({ | ||
data: {}, | ||
status: 'error', | ||
error: { code: 1002, message: 'not found' }, | ||
}), | ||
{ status: 401 } | ||
) | ||
|
||
expect(login('[email protected]', 'password')).rejects.toThrow( | ||
'Login failed' | ||
) | ||
}) | ||
|
||
test('server error', function () { | ||
fetchMock.mockReject() | ||
|
||
expect(login('[email protected]', 'password')).rejects.toThrow( | ||
'Login failed' | ||
) | ||
}) | ||
|
||
test('login successful', function () { | ||
const response = { | ||
code: 200, | ||
expire: '2022-10-01T18:22:37+02:00', | ||
token: 'token', | ||
} | ||
fetchMock.mockResponse(JSON.stringify(response), { status: 200 }) | ||
|
||
expect(login('[email protected]', 'password')).resolves.toEqual(response) | ||
}) | ||
}) |
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,41 @@ | ||
import { replaceMagic } from './helper' | ||
|
||
describe('helper', function () { | ||
test('replace links', function () { | ||
const text = 'https://www.systemli.org' | ||
|
||
expect(replaceMagic(text)).toBe( | ||
'<a href="https://www.systemli.org" target="_blank" rel="noopener noreferrer">www.systemli.org</a>' | ||
) | ||
}) | ||
|
||
test('replace twitter hashtag', function () { | ||
const text = '#hashtag' | ||
|
||
expect(replaceMagic(text)).toBe( | ||
'<a target="_blank" rel="noopener noreferrer" href="https://twitter.com/search?q=%23hashtag">#hashtag</a>' | ||
) | ||
}) | ||
|
||
test('replace twitter user handle', function () { | ||
const text = '/cc @systemli' | ||
|
||
expect(replaceMagic(text)).toBe( | ||
'/cc <a target="_blank" rel="noopener noreferrer" href="https://twitter.com/systemli">@systemli</a>' | ||
) | ||
}) | ||
|
||
test('replace email address', function () { | ||
const text = 'sent mail to [email protected]' | ||
|
||
expect(replaceMagic(text)).toBe( | ||
'sent mail to <a href="mailto:[email protected]">[email protected]</a>' | ||
) | ||
}) | ||
|
||
test('replace newline with <br/> tag', function () { | ||
const text = 'newline\nnewtext\r\n' | ||
|
||
expect(replaceMagic(text)).toBe('newline<br/>newtext<br/>') | ||
}) | ||
}) |
Oops, something went wrong.