|
| 1 | +const ScheduleEventTypeIsEventSubclass = require('./scheduleeventype-is-valid-event-subclass-rule'); |
| 2 | +const Model = require('../../classes/model'); |
| 3 | +const ModelNode = require('../../classes/model-node'); |
| 4 | +const ValidationErrorType = require('../../errors/validation-error-type'); |
| 5 | +const ValidationErrorSeverity = require('../../errors/validation-error-severity'); |
| 6 | + |
| 7 | +describe('ScheduleEventTypeIsEventSubclass', () => { |
| 8 | + let rule; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + rule = new ScheduleEventTypeIsEventSubclass(); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should target models of any type', () => { |
| 15 | + const model = new Model({ |
| 16 | + type: 'Schedule', |
| 17 | + }, 'latest'); |
| 18 | + |
| 19 | + const isTargeted = rule.isModelTargeted(model); |
| 20 | + expect(isTargeted).toBe(true); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should return no errors if scheduleEventType is a subClass of Event', async () => { |
| 24 | + const model = new Model({ |
| 25 | + type: 'Schedule', |
| 26 | + subClassGraph: ['#Event'], |
| 27 | + }, 'latest'); |
| 28 | + |
| 29 | + const data = { |
| 30 | + scheduledEventType: 'ScheduledSession', |
| 31 | + }; |
| 32 | + |
| 33 | + const nodeToTest = new ModelNode( |
| 34 | + '$', |
| 35 | + data, |
| 36 | + null, |
| 37 | + model, |
| 38 | + ); |
| 39 | + const errors = await rule.validate(nodeToTest); |
| 40 | + |
| 41 | + expect(errors.length).toBe(0); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should return errors if scheduleEventType is not a subClass of Event', async () => { |
| 45 | + const model = new Model({ |
| 46 | + type: 'Schedule', |
| 47 | + subClassGraph: ['#Event'], |
| 48 | + }, 'latest'); |
| 49 | + |
| 50 | + const data = { |
| 51 | + scheduledEventType: 'Place', |
| 52 | + }; |
| 53 | + |
| 54 | + const nodeToTest = new ModelNode( |
| 55 | + '$', |
| 56 | + data, |
| 57 | + null, |
| 58 | + model, |
| 59 | + ); |
| 60 | + const errors = await rule.validate(nodeToTest); |
| 61 | + |
| 62 | + expect(errors.length).toBe(1); |
| 63 | + for (const error of errors) { |
| 64 | + expect(error.type).toBe(ValidationErrorType.INVALID_SCHEDULE_EVENT_TYPE); |
| 65 | + expect(error.severity).toBe(ValidationErrorSeverity.FAILURE); |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + it('should return errors if scheduleEventType does not have a valid model', async () => { |
| 70 | + const model = new Model({ |
| 71 | + type: 'Schedule', |
| 72 | + subClassGraph: ['#Event'], |
| 73 | + }, 'latest'); |
| 74 | + |
| 75 | + const data = { |
| 76 | + scheduledEventType: 'Banana', |
| 77 | + }; |
| 78 | + |
| 79 | + const nodeToTest = new ModelNode( |
| 80 | + '$', |
| 81 | + data, |
| 82 | + null, |
| 83 | + model, |
| 84 | + ); |
| 85 | + const errors = await rule.validate(nodeToTest); |
| 86 | + |
| 87 | + expect(errors.length).toBe(1); |
| 88 | + for (const error of errors) { |
| 89 | + expect(error.type).toBe(ValidationErrorType.INVALID_SCHEDULE_EVENT_TYPE); |
| 90 | + expect(error.severity).toBe(ValidationErrorSeverity.FAILURE); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + it('should return errors if scheduleEventType does not have a subClassGraph', async () => { |
| 95 | + const model = new Model({ |
| 96 | + type: 'Schedule', |
| 97 | + subClassGraph: ['#Event'], |
| 98 | + }, 'latest'); |
| 99 | + |
| 100 | + const data = { |
| 101 | + scheduledEventType: 'DownloadData', |
| 102 | + }; |
| 103 | + |
| 104 | + const nodeToTest = new ModelNode( |
| 105 | + '$', |
| 106 | + data, |
| 107 | + null, |
| 108 | + model, |
| 109 | + ); |
| 110 | + const errors = await rule.validate(nodeToTest); |
| 111 | + |
| 112 | + expect(errors.length).toBe(1); |
| 113 | + for (const error of errors) { |
| 114 | + expect(error.type).toBe(ValidationErrorType.INVALID_SCHEDULE_EVENT_TYPE); |
| 115 | + expect(error.severity).toBe(ValidationErrorSeverity.FAILURE); |
| 116 | + } |
| 117 | + }); |
| 118 | +}); |
0 commit comments