backstage

Форк
0
/
readDurationFromConfig.test.ts 
128 строк · 3.9 Кб
1
/*
2
 * Copyright 2023 The Backstage Authors
3
 *
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
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
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.
15
 */
16

17
import {
18
  readDurationFromConfig,
19
  propsOfHumanDuration,
20
} from './readDurationFromConfig';
21
import { ConfigReader } from './reader';
22

23
describe('readDurationFromConfig', () => {
24
  it('reads all known keys', () => {
25
    const config = new ConfigReader({
26
      milliseconds: 1,
27
      seconds: 2,
28
      minutes: 3,
29
      hours: 4,
30
      days: 5,
31
      weeks: 6,
32
      months: 7,
33
      years: 8,
34
    });
35
    expect(readDurationFromConfig(config)).toEqual({
36
      milliseconds: 1,
37
      seconds: 2,
38
      minutes: 3,
39
      hours: 4,
40
      days: 5,
41
      weeks: 6,
42
      months: 7,
43
      years: 8,
44
    });
45
  });
46

47
  it('reads all known keys, for a subkey', () => {
48
    const config = new ConfigReader({
49
      sub: {
50
        key: {
51
          milliseconds: 1,
52
          seconds: 2,
53
          minutes: 3,
54
          hours: 4,
55
          days: 5,
56
          weeks: 6,
57
          months: 7,
58
          years: 8,
59
        },
60
      },
61
    });
62
    expect(readDurationFromConfig(config, { key: 'sub.key' })).toEqual({
63
      milliseconds: 1,
64
      seconds: 2,
65
      minutes: 3,
66
      hours: 4,
67
      days: 5,
68
      weeks: 6,
69
      months: 7,
70
      years: 8,
71
    });
72
  });
73

74
  it('rejects wrong type of target, for a subkey', () => {
75
    const config = new ConfigReader({
76
      sub: { key: 7 },
77
    });
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",
80
    );
81
  });
82

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'`,
87
    );
88
  });
89

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'`,
94
    );
95
  });
96

97
  it('rejects unknown keys', () => {
98
    const config = new ConfigReader({
99
      minutes: 3,
100
      invalid: 'value',
101
    });
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'`,
104
    );
105
  });
106

107
  it.each(propsOfHumanDuration)('rejects non-number %p', prop => {
108
    const config = new ConfigReader({
109
      [prop]: 'value',
110
    });
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`,
113
    );
114
  });
115

116
  it.each(propsOfHumanDuration)('rejects non-number %p, for a subkey', prop => {
117
    const config = new ConfigReader({
118
      sub: {
119
        key: {
120
          [prop]: 'value',
121
        },
122
      },
123
    });
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`,
126
    );
127
  });
128
});
129

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.