Skip to content

Commit

Permalink
fix(react): add assertion function to assert condition (#159)
Browse files Browse the repository at this point in the history
* fix(assert): add assertion function to assert condition

* Create spotty-rocks-dance.md

* Update .changeset/spotty-rocks-dance.md

* Update packages/react/src/utils/assert.ts

Co-authored-by: 박찬혁 <[email protected]>

* test(react): modify assertion test code. (#162)

* fix(react): add assertion function to assert conditionchange some test code

* fix(react): revert script

* Update packages/react/src/utils/assert.spec.ts

Co-authored-by: Jonghyeon Ko <[email protected]>

* Update packages/react/src/utils/assert.spec.ts

Co-authored-by: Jonghyeon Ko <[email protected]>

* Update packages/react/src/utils/assert.spec.ts

Co-authored-by: Jonghyeon Ko <[email protected]>

---------

Co-authored-by: Jonghyeon Ko <[email protected]>

---------

Co-authored-by: 박찬혁 <[email protected]>
  • Loading branch information
manudeli and okinawaa authored Sep 22, 2023
1 parent 08691d7 commit eac6832
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-rocks-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@suspensive/react": patch
---

fix(react): add assertion function to assert condition
34 changes: 34 additions & 0 deletions packages/react/src/utils/assert.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { assert } from '.'

function get(type: 'foo' | 'bar') {
switch (type) {
case 'foo':
return {
foo1: 'foo1',
foo2: 'foo2',
} as const
case 'bar':
return {
bar1: 'bar1',
bar2: 'bar2',
} as const
default: {
throw new Error('no type')
}
}
}

describe('assert', () => {
// eslint-disable-next-line vitest/expect-expect
it('should assert condition', () => {
const data = get('foo')
assert(data.foo1 === 'foo1', 'dummy-message')
expectTypeOf(data.foo2).toMatchTypeOf<string>()
expectTypeOf(data.bar1).not.toMatchTypeOf<string>()
})

it('should throw error if given condition is not met', () => {
const value = 'baz' as string
expect(() => assert(value === 'paz', "value should be 'paz'")).toThrow("value should be 'paz'")
})
})
5 changes: 5 additions & 0 deletions packages/react/src/utils/assert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function assert(condition: boolean, message: string): asserts condition {
if (!condition) {
throw new Error(message)
}
}
1 change: 1 addition & 0 deletions packages/react/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { hasResetKeysChanged } from './hasResetKeysChanged'
export { hashKey } from './hashKey'
export { assert } from './assert'

1 comment on commit eac6832

@vercel
Copy link

@vercel vercel bot commented on eac6832 Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

visualization – ./websites/visualization

visualization-git-main-suspensive.vercel.app
visualization.suspensive.org
visualization-suspensive.vercel.app

Please sign in to comment.