|
| 1 | +import { App, Stack } from '../../core'; |
| 2 | +import { Match, Tags } from '../lib'; |
| 3 | + |
| 4 | +describe('Tags', () => { |
| 5 | + let app: App; |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + app = new App(); |
| 9 | + }); |
| 10 | + |
| 11 | + describe('hasValues', () => { |
| 12 | + test('simple match', () => { |
| 13 | + const stack = new Stack(app, 'stack', { |
| 14 | + tags: { 'tag-one': 'tag-one-value' }, |
| 15 | + }); |
| 16 | + const tags = Tags.fromStack(stack); |
| 17 | + tags.hasValues({ |
| 18 | + 'tag-one': 'tag-one-value', |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + test('with matchers', () => { |
| 23 | + const stack = new Stack(app, 'stack', { |
| 24 | + tags: { 'tag-one': 'tag-one-value' }, |
| 25 | + }); |
| 26 | + const tags = Tags.fromStack(stack); |
| 27 | + tags.hasValues({ |
| 28 | + 'tag-one': Match.anyValue(), |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('given multiple tags', () => { |
| 33 | + const stack = new Stack(app, 'stack', { |
| 34 | + tags: { |
| 35 | + 'tag-one': 'tag-one-value', |
| 36 | + 'tag-two': 'tag-2-value', |
| 37 | + 'tag-three': 'tag-3-value', |
| 38 | + 'tag-four': 'tag-4-value', |
| 39 | + }, |
| 40 | + }); |
| 41 | + const tags = Tags.fromStack(stack); |
| 42 | + |
| 43 | + test('partial match succeeds', ()=>{ |
| 44 | + tags.hasValues({ |
| 45 | + 'tag-one': Match.anyValue(), |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + test('complex match succeeds', ()=>{ |
| 50 | + tags.hasValues(Match.objectEquals({ |
| 51 | + 'tag-one': Match.anyValue(), |
| 52 | + 'non-existent': Match.absent(), |
| 53 | + 'tag-three': Match.stringLikeRegexp('-3-'), |
| 54 | + 'tag-two': 'tag-2-value', |
| 55 | + 'tag-four': Match.anyValue(), |
| 56 | + })); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + test('no tags with absent matcher will fail', () => { |
| 61 | + const stack = new Stack(app, 'stack'); |
| 62 | + const tags = Tags.fromStack(stack); |
| 63 | + |
| 64 | + // Since the tags are defaulted to the empty object, using the `absent()` |
| 65 | + // matcher will never work, instead throwing an error. |
| 66 | + expect(() => tags.hasValues(Match.absent())).toThrow( |
| 67 | + /^match.absent\(\) will never match Tags/i, |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + test('no tags matches empty object successfully', () => { |
| 72 | + const stack = new Stack(app, 'stack'); |
| 73 | + const tags = Tags.fromStack(stack); |
| 74 | + |
| 75 | + tags.hasValues(Match.exact({})); |
| 76 | + }); |
| 77 | + |
| 78 | + test('no match', () => { |
| 79 | + const stack = new Stack(app, 'stack', { |
| 80 | + tags: { 'tag-one': 'tag-one-value' }, |
| 81 | + }); |
| 82 | + const tags = Tags.fromStack(stack); |
| 83 | + |
| 84 | + expect(() => |
| 85 | + tags.hasValues({ |
| 86 | + 'tag-one': 'mismatched value', |
| 87 | + }), |
| 88 | + ).toThrow(/Expected mismatched value but received tag-one-value/); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('hasNone', () => { |
| 93 | + test.each([undefined, {}])('matches empty: %s', (v) => { |
| 94 | + const stack = new Stack(app, 'stack', { tags: v }); |
| 95 | + const tags = Tags.fromStack(stack); |
| 96 | + |
| 97 | + tags.hasNone(); |
| 98 | + }); |
| 99 | + |
| 100 | + test.each(<Record<string, string>[]>[ |
| 101 | + { ['tagOne']: 'single-tag' }, |
| 102 | + { ['tagOne']: 'first-value', ['tag-two']: 'second-value' }, |
| 103 | + ])('does not match with values: %s', (v) => { |
| 104 | + const stack = new Stack(app, 'stack', { tags: v }); |
| 105 | + const tags = Tags.fromStack(stack); |
| 106 | + |
| 107 | + expect(() => tags.hasNone()).toThrow(/unexpected key/i); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('all', () => { |
| 112 | + test('simple match', () => { |
| 113 | + const stack = new Stack(app, 'stack', { |
| 114 | + tags: { 'tag-one': 'tag-one-value' }, |
| 115 | + }); |
| 116 | + const tags = Tags.fromStack(stack); |
| 117 | + expect(tags.all()).toStrictEqual({ |
| 118 | + 'tag-one': 'tag-one-value', |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + test('no tags', () => { |
| 123 | + const stack = new Stack(app, 'stack'); |
| 124 | + const tags = Tags.fromStack(stack); |
| 125 | + |
| 126 | + expect(tags.all()).toStrictEqual({}); |
| 127 | + }); |
| 128 | + |
| 129 | + test('empty tags', () => { |
| 130 | + const stack = new Stack(app, 'stack', { tags: {} }); |
| 131 | + const tags = Tags.fromStack(stack); |
| 132 | + |
| 133 | + expect(tags.all()).toStrictEqual({}); |
| 134 | + }); |
| 135 | + }); |
| 136 | +}); |
0 commit comments