From dba330bd479569e377abe7e3130e3c57e523aca1 Mon Sep 17 00:00:00 2001 From: Lukas Oppermann Date: Tue, 10 Oct 2023 13:42:07 +0200 Subject: [PATCH] add test for scope schema (#765) --- src/schemas/scopeTokenSchema.test.ts | 29 ++++++++++++++++++++++++++++ src/schemas/scopes.ts | 4 ++-- 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 src/schemas/scopeTokenSchema.test.ts diff --git a/src/schemas/scopeTokenSchema.test.ts b/src/schemas/scopeTokenSchema.test.ts new file mode 100644 index 000000000..aa3190a94 --- /dev/null +++ b/src/schemas/scopeTokenSchema.test.ts @@ -0,0 +1,29 @@ +import type {ValidScope} from './scopes' +import {scopes} from './scopes' + +const validScopes: ValidScope[] = ['all', 'bgColor', 'fgColor', 'borderColor', 'size', 'gap', 'radius'] +const scopeSchema = scopes(validScopes) + +describe('Schema: scopes', () => { + it('parses valid scopes', () => { + expect(scopeSchema.safeParse(['all']).success).toStrictEqual(true) + expect(scopeSchema.safeParse(['bgColor']).success).toStrictEqual(true) + expect(scopeSchema.safeParse(['fgColor']).success).toStrictEqual(true) + expect(scopeSchema.safeParse(['borderColor']).success).toStrictEqual(true) + expect(scopeSchema.safeParse(['size']).success).toStrictEqual(true) + expect(scopeSchema.safeParse(['gap']).success).toStrictEqual(true) + expect(scopeSchema.safeParse(['radius']).success).toStrictEqual(true) + expect( + scopeSchema.safeParse(['all', 'bgColor', 'fgColor', 'borderColor', 'size', 'gap', 'radius']).success, + ).toStrictEqual(true) + expect(scopeSchema.safeParse([]).success).toStrictEqual(true) + }) + + it('fails on invalid scope', () => { + expect(scopeSchema.safeParse(['invalid']).success).toStrictEqual(false) + expect(scopeSchema.safeParse(['']).success).toStrictEqual(false) + expect(scopeSchema.safeParse([1]).success).toStrictEqual(false) + expect(scopeSchema.safeParse([false]).success).toStrictEqual(false) + expect(scopeSchema.safeParse([undefined]).success).toStrictEqual(false) + }) +}) diff --git a/src/schemas/scopes.ts b/src/schemas/scopes.ts index c1a6f0d8d..a2522c490 100644 --- a/src/schemas/scopes.ts +++ b/src/schemas/scopes.ts @@ -2,8 +2,8 @@ import {z} from 'zod' import {joinFriendly} from '../utilities/joinFriendly' import {schemaErrorMessage} from '../utilities/schemaErrorMessage' -type ValidScope = 'all' | 'bgColor' | 'fgColor' | 'borderColor' | 'size' | 'gap' | 'radius' | 'borderColor' -const validScopes: ValidScope[] = ['all', 'bgColor', 'fgColor', 'borderColor', 'size', 'gap', 'radius', 'borderColor'] +export type ValidScope = 'all' | 'bgColor' | 'fgColor' | 'borderColor' | 'size' | 'gap' | 'radius' +const validScopes: ValidScope[] = ['all', 'bgColor', 'fgColor', 'borderColor', 'size', 'gap', 'radius'] export const scopes = (scopeSubset?: ValidScope[]) => { const scopeArray = scopeSubset ?? validScopes