|
| 1 | +import { create } from '@metamask/superstruct'; |
| 2 | + |
1 | 3 | import { |
| 4 | + CronjobSpecificationStruct, |
2 | 5 | isCronjobSpecification, |
3 | 6 | isCronjobSpecificationArray, |
4 | | - parseCronExpression, |
5 | 7 | } from './cronjob'; |
6 | 8 |
|
7 | | -describe('Cronjob Utilities', () => { |
8 | | - describe('isCronjobSpecification', () => { |
9 | | - it('returns true for a valid cronjob specification', () => { |
10 | | - const cronjobSpecification = { |
11 | | - expression: '* * * * *', |
| 9 | +describe('CronjobSpecificationStruct', () => { |
| 10 | + it.each([ |
| 11 | + ['100 * * * * *', 'Expected an object, but received: "100 * * * * *"'], |
| 12 | + ['PT10S', 'Expected an object, but received: "PT10S"'], |
| 13 | + [ |
| 14 | + { |
| 15 | + expression: '100 * * * * *', |
12 | 16 | request: { |
13 | 17 | method: 'exampleMethodOne', |
14 | 18 | params: ['p1'], |
15 | 19 | }, |
16 | | - }; |
17 | | - expect(isCronjobSpecification(cronjobSpecification)).toBe(true); |
18 | | - }); |
19 | | - |
20 | | - it('returns false for an invalid cronjob specification', () => { |
21 | | - const cronjobSpecification = { |
22 | | - expression: '* * * * * * * * * * *', |
| 20 | + }, |
| 21 | + 'At path: expression -- Expected a cronjob expression, but received: "100 * * * * *"', |
| 22 | + ], |
| 23 | + [ |
| 24 | + { |
| 25 | + duration: '10S', |
| 26 | + request: { |
| 27 | + method: 'exampleMethodOne', |
| 28 | + params: ['p1'], |
| 29 | + }, |
| 30 | + }, |
| 31 | + 'At path: duration -- Not a valid ISO 8601 duration', |
| 32 | + ], |
| 33 | + [ |
| 34 | + { |
23 | 35 | request: { |
24 | 36 | method: 'exampleMethodOne', |
25 | 37 | params: ['p1'], |
26 | 38 | }, |
27 | | - }; |
28 | | - expect(isCronjobSpecification(cronjobSpecification)).toBe(false); |
29 | | - }); |
| 39 | + }, |
| 40 | + 'At path: expression -- Expected a string, but received: undefined', |
| 41 | + ], |
| 42 | + ])('return a readable error for %p', (value, error) => { |
| 43 | + expect(() => create(value, CronjobSpecificationStruct)).toThrow(error); |
30 | 44 | }); |
| 45 | +}); |
31 | 46 |
|
32 | | - describe('isCronjobSpecificationArray', () => { |
33 | | - it('returns true for a valid cronjob specification array', () => { |
34 | | - const cronjobSpecificationArray = [ |
35 | | - { |
36 | | - expression: '* * * * *', |
37 | | - request: { |
38 | | - method: 'exampleMethodOne', |
39 | | - params: ['p1'], |
40 | | - }, |
41 | | - }, |
42 | | - ]; |
43 | | - expect(isCronjobSpecificationArray(cronjobSpecificationArray)).toBe(true); |
44 | | - }); |
| 47 | +describe('isCronjobSpecification', () => { |
| 48 | + it('returns true for a valid cronjob specification with an expression', () => { |
| 49 | + const cronjobSpecification = { |
| 50 | + expression: '* * * * *', |
| 51 | + request: { |
| 52 | + method: 'exampleMethodOne', |
| 53 | + params: ['p1'], |
| 54 | + }, |
| 55 | + }; |
| 56 | + expect(isCronjobSpecification(cronjobSpecification)).toBe(true); |
| 57 | + }); |
45 | 58 |
|
46 | | - it('returns false for an invalid cronjob specification array', () => { |
47 | | - const cronjobSpecificationArray = { |
| 59 | + it('returns true for a valid cronjob specification with a duration', () => { |
| 60 | + const cronjobSpecification = { |
| 61 | + duration: 'PT10S', |
| 62 | + request: { |
| 63 | + method: 'exampleMethodOne', |
| 64 | + params: ['p1'], |
| 65 | + }, |
| 66 | + }; |
| 67 | + expect(isCronjobSpecification(cronjobSpecification)).toBe(true); |
| 68 | + }); |
| 69 | + |
| 70 | + it('returns false for an invalid cronjob specification', () => { |
| 71 | + const cronjobSpecification = { |
| 72 | + expression: '* * * * * * * * * * *', |
| 73 | + request: { |
| 74 | + method: 'exampleMethodOne', |
| 75 | + params: ['p1'], |
| 76 | + }, |
| 77 | + }; |
| 78 | + expect(isCronjobSpecification(cronjobSpecification)).toBe(false); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +describe('isCronjobSpecificationArray', () => { |
| 83 | + it('returns true for a valid cronjob specification array', () => { |
| 84 | + const cronjobSpecificationArray = [ |
| 85 | + { |
48 | 86 | expression: '* * * * *', |
49 | 87 | request: { |
50 | 88 | method: 'exampleMethodOne', |
51 | 89 | params: ['p1'], |
52 | 90 | }, |
53 | | - }; |
54 | | - expect(isCronjobSpecificationArray(cronjobSpecificationArray)).toBe( |
55 | | - false, |
56 | | - ); |
57 | | - }); |
| 91 | + }, |
| 92 | + { |
| 93 | + duration: 'PT10S', |
| 94 | + request: { |
| 95 | + method: 'exampleMethodOne', |
| 96 | + params: ['p1'], |
| 97 | + }, |
| 98 | + }, |
| 99 | + ]; |
| 100 | + expect(isCronjobSpecificationArray(cronjobSpecificationArray)).toBe(true); |
58 | 101 | }); |
59 | 102 |
|
60 | | - describe('parseCronExpression', () => { |
61 | | - it('successfully parses cronjob expression that is provided as a string', () => { |
62 | | - const cronjobExpression = '* * * * *'; |
63 | | - |
64 | | - const parsedExpression = parseCronExpression(cronjobExpression); |
65 | | - expect(parsedExpression.next()).toBeDefined(); |
66 | | - expect(typeof parsedExpression.next().getTime()).toBe('number'); |
67 | | - }); |
| 103 | + it('returns false for an invalid cronjob specification array', () => { |
| 104 | + const cronjobSpecificationArray = { |
| 105 | + expression: '* * * * *', |
| 106 | + request: { |
| 107 | + method: 'exampleMethodOne', |
| 108 | + params: ['p1'], |
| 109 | + }, |
| 110 | + }; |
| 111 | + expect(isCronjobSpecificationArray(cronjobSpecificationArray)).toBe(false); |
68 | 112 | }); |
69 | 113 | }); |
0 commit comments