2
* Copyright 2023 The Backstage Authors
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
18
readDurationFromConfig,
20
} from './readDurationFromConfig';
21
import { ConfigReader } from './reader';
23
describe('readDurationFromConfig', () => {
24
it('reads all known keys', () => {
25
const config = new ConfigReader({
35
expect(readDurationFromConfig(config)).toEqual({
47
it('reads all known keys, for a subkey', () => {
48
const config = new ConfigReader({
62
expect(readDurationFromConfig(config, { key: 'sub.key' })).toEqual({
74
it('rejects wrong type of target, for a subkey', () => {
75
const config = new ConfigReader({
78
expect(() => readDurationFromConfig(config, { key: 'sub.key' })).toThrow(
79
"Failed to read duration from config, TypeError: Invalid type in config for key 'sub.key' in 'mock-config', got number, wanted object",
83
it('rejects no keys', () => {
84
const config = new ConfigReader({});
85
expect(() => readDurationFromConfig(config)).toThrow(
86
`Failed to read duration from config, Error: Needs one or more of 'years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds', 'milliseconds'`,
90
it('rejects no keys, for a subkey', () => {
91
const config = new ConfigReader({ sub: { key: {} } });
92
expect(() => readDurationFromConfig(config, { key: 'sub.key' })).toThrow(
93
`Failed to read duration from config at 'sub.key', Error: Needs one or more of 'years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds', 'milliseconds'`,
97
it('rejects unknown keys', () => {
98
const config = new ConfigReader({
102
expect(() => readDurationFromConfig(config)).toThrow(
103
`Failed to read duration from config, Error: Unknown property 'invalid'; expected one or more of 'years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds', 'milliseconds'`,
107
it.each(propsOfHumanDuration)('rejects non-number %p', prop => {
108
const config = new ConfigReader({
111
expect(() => readDurationFromConfig(config)).toThrow(
112
`Failed to read duration from config, Error: Unable to convert config value for key '${prop}' in 'mock-config' to a number`,
116
it.each(propsOfHumanDuration)('rejects non-number %p, for a subkey', prop => {
117
const config = new ConfigReader({
124
expect(() => readDurationFromConfig(config, { key: 'sub.key' })).toThrow(
125
`Failed to read duration from config, Error: Unable to convert config value for key 'sub.key.${prop}' in 'mock-config' to a number`,