Skip to content

Commit

Permalink
feat(utils): add assert helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Labat committed Feb 5, 2022
1 parent 58684a7 commit 05c3fe1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/core/tests/utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
parseMutationArgs,
matchMutation,
scheduleMicrotask,
assert,
} from '../utils'
import { QueryClient, QueryCache, setLogger, Logger } from '../..'
import { queryKey } from '../../reactjs/tests/utils'
Expand Down Expand Up @@ -389,4 +390,24 @@ describe('core/utils', () => {
jest.useRealTimers()
})
})

describe('assert', () => {
it('should assert acccording to condition', () => {
const logger: Logger = {
error: jest.fn(),
log: jest.fn(),
warn: jest.fn(),
}
setLogger(logger)

assert(true, '')
expect(logger.warn).not.toHaveBeenCalled()
assert(false, '')
expect(logger.warn).toHaveBeenCalledTimes(1)
assert(false, '')
expect(logger.warn).toHaveBeenCalledTimes(2)

setLogger(console)
})
})
})
10 changes: 10 additions & 0 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getLogger } from './logger'
import type { Mutation } from './mutation'
import type { Query } from './query'
import type {
Expand Down Expand Up @@ -74,6 +75,15 @@ export function noop(): undefined {
return undefined
}

/**
* Raise a dev warning message when the condition is not fulfilled
*/
export function assert(condition: boolean, message: string): void {
if (process.env.NODE_ENV !== 'production' && !condition) {
getLogger().warn(message)
}
}

export function functionalUpdate<TInput, TOutput>(
updater: Updater<TInput, TOutput>,
input: TInput
Expand Down

0 comments on commit 05c3fe1

Please sign in to comment.