Skip to content

Commit

Permalink
test(bezier-react): add test code to assert (channel-io#1770)
Browse files Browse the repository at this point in the history
<!--
  How to write a good PR title:
- Follow [the Conventional Commits
specification](https://www.conventionalcommits.org/en/v1.0.0/).
  - Give as much context as necessary and as little as possible
  - Prefix it with [WIP] while it’s a work in progress
-->

## Self Checklist

- [x] I wrote a PR title in **English** and added an appropriate
**label** to the PR.
- [x] I wrote the commit message in **English** and to follow [**the
Conventional Commits
specification**](https://www.conventionalcommits.org/en/v1.0.0/).
- [ ] I [added the
**changeset**](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md)
about the changes that needed to be released. (or didn't have to)
- [x] I wrote or updated **documentation** related to the changes. (or
didn't have to)
- [x] I wrote or updated **tests** related to the changes. (or didn't
have to)
- [x] I tested the changes in various browsers. (or didn't have to)
  - Windows: Chrome, Edge, (Optional) Firefox
  - macOS: Chrome, Edge, Safari, (Optional) Firefox

## Related Issue
<!-- Please link to issue if one exists -->

<!-- Fixes #0000 -->

## Summary
<!-- Please brief explanation of the changes made -->
add test code to assert

## Details
<!-- Please elaborate description of the changes -->
<img width="636" alt="image"
src="https://github.com/channel-io/bezier-react/assets/102217654/8672d1ae-d8ce-4b33-997e-149be16963a7">


### Breaking change? (Yes/No)
<!-- If Yes, please describe the impact and migration path for users -->

## References
<!-- Please list any other resources or points the reviewer should be
aware of -->

---------

Co-authored-by: Ed <[email protected]>
  • Loading branch information
SEOKKAMONI and sungik-choi authored Dec 7, 2023
1 parent 9b76e0f commit 678099a
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 2 deletions.
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 678099a

Please sign in to comment.