|
| 1 | +const Not = require('../../../lib/validators/not') |
| 2 | +const Helper = require('../../../__fixtures__/unit/helper') |
| 3 | + |
| 4 | +describe('Not Validator Unit Test', () => { |
| 5 | + let registry = { validators: new Map(), actions: new Map() } |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + registry = { validators: new Map(), actions: new Map() } |
| 9 | + }) |
| 10 | + |
| 11 | + test('Should pass if subtask fails', async () => { |
| 12 | + const not = new Not() |
| 13 | + const settings = { |
| 14 | + do: 'not', |
| 15 | + validate: [ |
| 16 | + { |
| 17 | + do: 'milestone', |
| 18 | + must_include: { |
| 19 | + regex: 'Version 2' |
| 20 | + } |
| 21 | + } |
| 22 | + ] |
| 23 | + } |
| 24 | + const validation = await not.processValidate(createMockContext({ title: 'Version 1' }), settings, registry) |
| 25 | + expect(validation.status).toBe('pass') |
| 26 | + }) |
| 27 | + |
| 28 | + test('Should fail if subtask passes', async () => { |
| 29 | + const and = new Not() |
| 30 | + const settings = { |
| 31 | + do: 'not', |
| 32 | + validate: [ |
| 33 | + { |
| 34 | + do: 'milestone', |
| 35 | + must_include: { |
| 36 | + regex: 'Version 2' |
| 37 | + } |
| 38 | + } |
| 39 | + ] |
| 40 | + } |
| 41 | + const validation = await and.processValidate(createMockContext({ title: 'Version 2' }), settings, registry) |
| 42 | + expect(validation.status).toBe('fail') |
| 43 | + }) |
| 44 | + |
| 45 | + test('Error is returned when validate has more than one item', async () => { |
| 46 | + const and = new Not() |
| 47 | + const settings = { |
| 48 | + do: 'not', |
| 49 | + validate: [ |
| 50 | + { |
| 51 | + do: 'milestone', |
| 52 | + must_include: { |
| 53 | + regex: 'Version 2' |
| 54 | + } |
| 55 | + }, |
| 56 | + { |
| 57 | + do: 'milestone', |
| 58 | + must_include: { |
| 59 | + regex: 'Version 1' |
| 60 | + } |
| 61 | + } |
| 62 | + ] |
| 63 | + } |
| 64 | + const validation = await and.processValidate(createMockContext({ title: 'Version 1' }), settings, registry) |
| 65 | + expect(validation.status).toBe('error') |
| 66 | + }) |
| 67 | + |
| 68 | + test('Error is returned when validate is missing', async () => { |
| 69 | + const and = new Not() |
| 70 | + const settings = { |
| 71 | + do: 'not' |
| 72 | + } |
| 73 | + const validation = await and.processValidate(createMockContext({ title: 'Version 1' }), settings, registry) |
| 74 | + expect(validation.status).toBe('error') |
| 75 | + }) |
| 76 | + |
| 77 | + test('Error is returned when validate is not an array', async () => { |
| 78 | + const and = new Not() |
| 79 | + const settings = { |
| 80 | + do: 'not', |
| 81 | + validate: '' |
| 82 | + } |
| 83 | + const validation = await and.processValidate(createMockContext({ title: 'Version 1' }), settings, registry) |
| 84 | + expect(validation.status).toBe('error') |
| 85 | + }) |
| 86 | + |
| 87 | + test('Error is returned when validate is empty', async () => { |
| 88 | + const and = new Not() |
| 89 | + const settings = { |
| 90 | + do: 'and', |
| 91 | + validate: [] |
| 92 | + } |
| 93 | + const validation = await and.processValidate(createMockContext({ title: 'Version 1' }), settings, registry) |
| 94 | + expect(validation.status).toBe('error') |
| 95 | + }) |
| 96 | + |
| 97 | + test('Error is returned when validate uses unsupported classes', async () => { |
| 98 | + const and = new Not() |
| 99 | + const settings = { |
| 100 | + do: 'and', |
| 101 | + validate: [ |
| 102 | + { do: 'missing' } |
| 103 | + ] |
| 104 | + } |
| 105 | + const validation = await and.processValidate(createMockContext({ title: 'Version 1' }), settings, registry) |
| 106 | + expect(validation.status).toBe('error') |
| 107 | + }) |
| 108 | + |
| 109 | + test('Error if the sub validator errored', async () => { |
| 110 | + const and = new Not() |
| 111 | + const settings = { |
| 112 | + do: 'and', |
| 113 | + validate: [ |
| 114 | + { |
| 115 | + do: 'and', |
| 116 | + validate: [ |
| 117 | + { |
| 118 | + do: 'milestone', |
| 119 | + // invalid syntax => error |
| 120 | + must_inclxude: { |
| 121 | + regex: 'Version 1' |
| 122 | + } |
| 123 | + }, |
| 124 | + { |
| 125 | + do: 'milestone', |
| 126 | + must_include: { |
| 127 | + regex: 'Version 2' |
| 128 | + } |
| 129 | + } |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + do: 'milestone', |
| 134 | + must_include: { |
| 135 | + regex: 'Version 3' |
| 136 | + } |
| 137 | + } |
| 138 | + ] |
| 139 | + } |
| 140 | + |
| 141 | + const validation = await and.processValidate(createMockContext({ title: 'Version 2' }), settings, registry) |
| 142 | + expect(validation.status).toBe('error') |
| 143 | + }) |
| 144 | +}) |
| 145 | + |
| 146 | +const createMockContext = (milestone, body, deepValidation) => { |
| 147 | + return Helper.mockContext({ milestone, body, deepValidation }) |
| 148 | +} |
0 commit comments