|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import {normalizePluginOptions} from '@docusaurus/utils-validation'; |
| 9 | +import { |
| 10 | + validateOptions, |
| 11 | + type PluginOptions, |
| 12 | + type Options, |
| 13 | + DEFAULT_OPTIONS, |
| 14 | +} from '../options'; |
| 15 | +import type {Validate} from '@docusaurus/types'; |
| 16 | + |
| 17 | +function validate(options?: Options) { |
| 18 | + return validateOptions({ |
| 19 | + validate: normalizePluginOptions as Validate< |
| 20 | + Options | undefined, |
| 21 | + PluginOptions |
| 22 | + >, |
| 23 | + options, |
| 24 | + }); |
| 25 | +} |
| 26 | + |
| 27 | +function result(options?: Options) { |
| 28 | + return { |
| 29 | + id: 'default', |
| 30 | + ...DEFAULT_OPTIONS, |
| 31 | + ...options, |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +describe('validateOptions', () => { |
| 36 | + it('accepts undefined', () => { |
| 37 | + expect(validate(undefined)).toEqual(result(DEFAULT_OPTIONS)); |
| 38 | + }); |
| 39 | + |
| 40 | + it('accepts empty object', () => { |
| 41 | + expect(validate({})).toEqual(result(DEFAULT_OPTIONS)); |
| 42 | + }); |
| 43 | + |
| 44 | + it('accepts defaults', () => { |
| 45 | + expect(validate(DEFAULT_OPTIONS)).toEqual(result(DEFAULT_OPTIONS)); |
| 46 | + }); |
| 47 | + |
| 48 | + it('rejects null', () => { |
| 49 | + expect( |
| 50 | + // @ts-expect-error: TS should error |
| 51 | + () => validate(null), |
| 52 | + ).toThrowErrorMatchingInlineSnapshot(`""value" must be of type object"`); |
| 53 | + }); |
| 54 | + |
| 55 | + it('rejects number', () => { |
| 56 | + expect( |
| 57 | + // @ts-expect-error: TS should error |
| 58 | + () => validate(42), |
| 59 | + ).toThrowErrorMatchingInlineSnapshot(`""value" must be of type object"`); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('svgrConfig', () => { |
| 63 | + it('accepts undefined', () => { |
| 64 | + expect(validate({svgrConfig: undefined})).toEqual( |
| 65 | + result(DEFAULT_OPTIONS), |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + it('accepts empty', () => { |
| 70 | + expect(validate({svgrConfig: {}})).toEqual(result(DEFAULT_OPTIONS)); |
| 71 | + }); |
| 72 | + |
| 73 | + it('accepts any record', () => { |
| 74 | + expect(validate({svgrConfig: {any: 'value', evenNumbers: 42}})).toEqual( |
| 75 | + result({ |
| 76 | + ...DEFAULT_OPTIONS, |
| 77 | + svgrConfig: { |
| 78 | + any: 'value', |
| 79 | + evenNumbers: 42, |
| 80 | + }, |
| 81 | + }), |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + it('accepts default', () => { |
| 86 | + expect(validate({svgrConfig: DEFAULT_OPTIONS.svgrConfig})).toEqual( |
| 87 | + result(DEFAULT_OPTIONS), |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + it('rejects number values', () => { |
| 92 | + expect(() => |
| 93 | + // @ts-expect-error: invalid type |
| 94 | + validate({svgrConfig: 42}), |
| 95 | + ).toThrowErrorMatchingInlineSnapshot( |
| 96 | + `""svgrConfig" must be of type object"`, |
| 97 | + ); |
| 98 | + }); |
| 99 | + }); |
| 100 | +}); |
0 commit comments