Skip to content

Commit

Permalink
Merge branch 'main' into alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
sungik-choi committed Dec 12, 2023
2 parents d1e0fc7 + 45dee29 commit b5cacba
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 14 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
jobs:
release:
name: Release
if: github.repository == 'channel-io/bezier-react'
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand Down
11 changes: 11 additions & 0 deletions packages/bezier-react/src/hooks/useIsMounted.test.ts
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)
})
})
39 changes: 28 additions & 11 deletions packages/bezier-react/src/utils/array.test.ts
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])
})
})
2 changes: 1 addition & 1 deletion packages/bezier-react/src/utils/array.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isArray } from './type'

export function isLastIndex(array: any[], index: number) {
export function isLastIndex(array: ArrayLike<unknown>, index: number) {
return array.length - 1 === index
}

Expand Down
92 changes: 92 additions & 0 deletions packages/bezier-react/src/utils/assert.test.ts
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()
})
})
4 changes: 2 additions & 2 deletions packages/bezier-react/src/utils/assert.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function isDev() {
export function isDev() {
return process.env.NODE_ENV !== 'production'
}

Expand All @@ -9,7 +9,7 @@ export function warn(message: string) {
}
}

class AssertionException extends Error {
export class AssertionException extends Error {
constructor(message?: string) {
super()

Expand Down

0 comments on commit b5cacba

Please sign in to comment.