-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
135 additions
and
14 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,11 @@ | ||
import { renderHook } from '@testing-library/react' | ||
|
||
import useIsMounted from './useIsMounted' | ||
|
||
describe('useIsMounted', () => { | ||
it('should return true when mounted', () => { | ||
const { result } = renderHook(() => useIsMounted()) | ||
|
||
expect(result.current).toBe(true) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,20 +1,37 @@ | ||
import { | ||
compact, | ||
flattenDeep, | ||
isLastIndex, | ||
} from './array' | ||
|
||
describe('arrayUtils', () => { | ||
describe('compact', () => { | ||
it('should remove falsy item', () => { | ||
const array = [0, 1, false, 2, '', 3] | ||
expect(compact(array)).toEqual([1, 2, 3]) | ||
}) | ||
describe('isLastIndex', () => { | ||
it('should return true when the last array index', () => { | ||
const arr = [0, 1, 2, 3, 4] | ||
|
||
const result = isLastIndex(arr, 4) | ||
|
||
expect(result).toBe(true) | ||
}) | ||
|
||
it('should return false when not the last array index', () => { | ||
const arr = [0, 1, 2, 3, 4] | ||
|
||
const result = isLastIndex(arr, 1) | ||
|
||
expect(result).toBe(false) | ||
}) | ||
}) | ||
|
||
describe('compact', () => { | ||
it('should remove falsy item', () => { | ||
const array = [0, 1, false, 2, '', 3] | ||
expect(compact(array)).toEqual([1, 2, 3]) | ||
}) | ||
}) | ||
|
||
describe('flattenDeep', () => { | ||
it('should return flatten array', () => { | ||
const array = [1, [2, [3, [4]], 5]] | ||
expect(flattenDeep(array)).toEqual([1, 2, 3, 4, 5]) | ||
}) | ||
describe('flattenDeep', () => { | ||
it('should return flatten array', () => { | ||
const array = [1, [2, [3, [4]], 5]] | ||
expect(flattenDeep(array)).toEqual([1, 2, 3, 4, 5]) | ||
}) | ||
}) |
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,92 @@ | ||
import { | ||
AssertionException, | ||
assert, | ||
isDev, | ||
warn, | ||
} from './assert' | ||
|
||
describe('isDev', () => { | ||
const ORIGINAL_ENV = process.env | ||
|
||
beforeEach(() => { | ||
jest.resetModules() | ||
process.env = { ...ORIGINAL_ENV } | ||
}) | ||
|
||
afterAll(() => { | ||
process.env = ORIGINAL_ENV | ||
}) | ||
|
||
it('should return true when NODE_ENV is not production', () => { | ||
process.env.NODE_ENV = 'development' | ||
|
||
const result = isDev() | ||
|
||
expect(result).toBe(true) | ||
}) | ||
|
||
it('should return false when NODE_ENV is production', () => { | ||
process.env.NODE_ENV = 'production' | ||
|
||
const result = isDev() | ||
|
||
expect(result).toBe(false) | ||
}) | ||
}) | ||
|
||
describe('assert', () => { | ||
const ORIGINAL_ENV = process.env | ||
|
||
beforeEach(() => { | ||
jest.resetModules() | ||
process.env = { ...ORIGINAL_ENV } | ||
}) | ||
|
||
afterAll(() => { | ||
process.env = ORIGINAL_ENV | ||
}) | ||
|
||
it('should throw an assertion exception error in development environment when the predicate is false', () => { | ||
process.env.NODE_ENV = 'development' | ||
|
||
expect(() => assert(false)).toThrow(new AssertionException()) | ||
}) | ||
|
||
it('should throw an "failed" with a error in development environment when the predicate is false', () => { | ||
process.env.NODE_ENV = 'development' | ||
|
||
expect(() => assert(false, 'failed')).toThrow(new AssertionException('failed')) | ||
}) | ||
|
||
it('should not throw in production environment when the predicate is true', () => { | ||
process.env.NODE_ENV = 'production' | ||
|
||
expect(() => assert(true)).not.toThrow(new AssertionException()) | ||
}) | ||
}) | ||
|
||
describe('warn', () => { | ||
const ORIGINAL_ENV = process.env | ||
|
||
beforeEach(() => { | ||
jest.resetModules() | ||
process.env = { ...ORIGINAL_ENV } | ||
}) | ||
|
||
afterAll(() => { | ||
process.env = ORIGINAL_ENV | ||
}) | ||
|
||
it('should output the message using console.warn if the message argument is passed', () => { | ||
process.env.NODE_ENV = 'development' | ||
|
||
const warnSpy = jest.spyOn(console, 'warn') | ||
|
||
warn('Warn') | ||
|
||
expect(warnSpy).toHaveBeenCalled() | ||
expect(warnSpy).toHaveBeenCalledWith('Warn') | ||
|
||
warnSpy.mockRestore() | ||
}) | ||
}) |
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