|
1 | 1 | import { describe, expect, test } from '@jest/globals'; |
2 | 2 | import fs from 'fs'; |
3 | 3 | import path from 'path'; |
| 4 | +import { z } from 'zod'; |
4 | 5 | import { GetOptionsPreferences, OptionsProvider, OptionsWatcher } from "../dist/index"; |
5 | 6 |
|
6 | 7 | const runSuite = (suitePath: string) => { |
@@ -33,6 +34,62 @@ const runSuite = (suitePath: string) => { |
33 | 34 | } |
34 | 35 | } |
35 | 36 |
|
| 37 | +const DeeperObjectSchema = z.object({ |
| 38 | + wtv: z.number(), |
| 39 | + list: z.array(z.number()), |
| 40 | +}); |
| 41 | + |
| 42 | +const MyObjectSchema = z.object({ |
| 43 | + one: z.number(), |
| 44 | + two: z.number(), |
| 45 | + string: z.string(), |
| 46 | + deeper: DeeperObjectSchema, |
| 47 | +}); |
| 48 | + |
| 49 | +const MyConfigSchema = z.object({ |
| 50 | + rootString: z.string(), |
| 51 | + rootString2: z.string(), |
| 52 | + myArray: z.array(z.string()), |
| 53 | + myObject: MyObjectSchema, |
| 54 | +}); |
| 55 | + |
| 56 | +describe('getOptions', () => { |
| 57 | + const configsPath = path.join(__dirname, '../../../tests/test_suites/simple/configs'); |
| 58 | + const providers = [{ |
| 59 | + name: "OptionsProvider", |
| 60 | + provider: OptionsProvider.build(configsPath), |
| 61 | + }, { |
| 62 | + name: "OptionsWatcher", |
| 63 | + provider: OptionsWatcher.build(configsPath), |
| 64 | + }]; |
| 65 | + |
| 66 | + for (const { name, provider } of providers) { |
| 67 | + test(`${name} validates and returns a typed object with a schema`, () => { |
| 68 | + const config = provider.getOptions('myConfig', ['A'], MyConfigSchema); |
| 69 | + expect(config.rootString).toBe('root string same'); |
| 70 | + expect(config.rootString2).toBe('gets overridden'); |
| 71 | + expect(config.myArray).toEqual(['example item 1']); |
| 72 | + expect(config.myObject.one).toBe(1); |
| 73 | + expect(config.myObject.deeper.list).toEqual([1, 2]); |
| 74 | + }); |
| 75 | + |
| 76 | + test(`${name} getOptions with schema matches getOptionsJson`, () => { |
| 77 | + const preferences = new GetOptionsPreferences(); |
| 78 | + preferences.enableConfigurableStrings(); |
| 79 | + const fromJson = JSON.parse(provider.getOptionsJson('myConfig', ['A'], preferences)); |
| 80 | + const fromGetOptions = provider.getOptions('myConfig', ['A'], MyConfigSchema, preferences); |
| 81 | + expect(fromGetOptions).toEqual(fromJson); |
| 82 | + }); |
| 83 | + |
| 84 | + test(`${name} getOptions with schema rejects invalid data`, () => { |
| 85 | + const StrictSchema = z.object({ |
| 86 | + rootString: z.number(), |
| 87 | + }); |
| 88 | + expect(() => provider.getOptions('myConfig', ['A'], StrictSchema)).toThrow(); |
| 89 | + }); |
| 90 | + } |
| 91 | +}); |
| 92 | + |
36 | 93 | const testSuitesDir = path.join(__dirname, '../../../tests/test_suites'); |
37 | 94 | for (const suite of fs.readdirSync(testSuitesDir)) { |
38 | 95 | describe(`Suite: ${suite}`, () => { |
|
0 commit comments