mirror of
https://github.com/PostHog/posthog.git
synced 2024-11-22 08:40:03 +01:00
44e67095a0
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
export class AsymmetricMatcher<T> {
|
|
protected sample: T
|
|
$$typeof: symbol
|
|
inverse?: boolean
|
|
|
|
constructor(sample: T) {
|
|
this.$$typeof = Symbol.for('jest.asymmetricMatcher')
|
|
this.sample = sample
|
|
}
|
|
}
|
|
|
|
class Truth extends AsymmetricMatcher<(value: any) => boolean> {
|
|
constructor(sample: (value: any) => boolean, inverse: boolean = false) {
|
|
if (typeof sample !== 'function') {
|
|
throw new Error('Expected is not a function')
|
|
}
|
|
super(sample)
|
|
this.inverse = inverse
|
|
}
|
|
|
|
asymmetricMatch(other: any): boolean {
|
|
const result = this.sample(other)
|
|
|
|
return this.inverse ? !result : result
|
|
}
|
|
|
|
toString(): string {
|
|
return `${this.inverse ? 'Not' : ''}Truth`
|
|
}
|
|
|
|
toAsymmetricMatcher(): string {
|
|
return `Truth<${this.sample}>`
|
|
}
|
|
}
|
|
|
|
export const truth = (sample: (value: any) => boolean): Truth => new Truth(sample)
|
|
|
|
export function partial<T>(objectOrArray: T): T {
|
|
if (Array.isArray(objectOrArray)) {
|
|
return expect.arrayContaining(objectOrArray)
|
|
} else {
|
|
return expect.objectContaining(objectOrArray)
|
|
}
|
|
}
|