Skip to content

Commit 36f7e39

Browse files
committed
Refactor isUser tests to use createUser, getAllFieldCombinations
1 parent 2e097d8 commit 36f7e39

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

types/user.test.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import { describe, it } from 'jsr:@std/testing/bdd'
22
import { expect } from 'jsr:@std/expect'
3-
import { isUser } from './user.ts'
3+
import getAllFieldCombinations from '../utils/testing/get-all-field-combinations.ts'
4+
import { isUser, createUser } from './user.ts'
45

56
describe('isUser', () => {
6-
const id = crypto.randomUUID()
7-
const name = 'John Doe'
8-
const username = 'john'
9-
const roles = ['active', 'listed']
10-
117
it('returns false if given a primitive', () => {
128
const primitives = [() => {}, null, undefined, true, false, 'test', 1]
139
for (const primitive of primitives) {
@@ -20,17 +16,15 @@ describe('isUser', () => {
2016
})
2117

2218
it('returns true if given a valid User', () => {
23-
expect(isUser({ name })).toBe(true)
24-
expect(isUser({ id, name })).toBe(true)
25-
expect(isUser({ name, username })).toBe(true)
26-
expect(isUser({ name, roles })).toBe(true)
27-
expect(isUser({ id, name, username })).toBe(true)
28-
expect(isUser({ id, name, roles })).toBe(true)
29-
expect(isUser({ name, username, roles })).toBe(true)
30-
expect(isUser({ id, name, username, roles })).toBe(true)
19+
const required = ['name']
20+
const objects = getAllFieldCombinations(createUser())
21+
for (const object of objects) {
22+
const includesAllRequired = required.every(key => Object.keys(object).includes(key))
23+
expect(isUser(object)).toBe(includesAllRequired)
24+
}
3125
})
3226

3327
it('returns false if given additional properties', () => {
34-
expect(isUser({ name, other: 'nope' })).toBe(false)
28+
expect(isUser({ ...createUser(), other: 'nope' })).toBe(false)
3529
})
3630
})

0 commit comments

Comments
 (0)