|
| 1 | +import { PassageEventInstantiationError } from '../../../../../../src/devcomp/domain/errors.js'; |
| 2 | +import { PassageEvent } from '../../../../../../src/devcomp/domain/models/passage-events/PassageEvent.js'; |
| 3 | +import { DomainError } from '../../../../../../src/shared/domain/errors.js'; |
| 4 | +import { catchErrSync, expect } from '../../../../../test-helper.js'; |
| 5 | + |
| 6 | +describe('Unit | Devcomp | Domain | Models | PassageEvent', function () { |
| 7 | + describe('#constructor', function () { |
| 8 | + it('should not be able to create a PassageEvent directly', function () { |
| 9 | + // given & when |
| 10 | + const error = catchErrSync(() => new PassageEvent({}))(); |
| 11 | + |
| 12 | + // then |
| 13 | + expect(error).to.be.instanceOf(PassageEventInstantiationError); |
| 14 | + }); |
| 15 | + |
| 16 | + describe('if a passage event does not have an id', function () { |
| 17 | + it('should throw an error', function () { |
| 18 | + // given |
| 19 | + class FakeEvent extends PassageEvent { |
| 20 | + constructor() { |
| 21 | + super({}); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + // when |
| 26 | + const error = catchErrSync(() => new FakeEvent())(); |
| 27 | + |
| 28 | + // then |
| 29 | + expect(error).to.be.instanceOf(DomainError); |
| 30 | + expect(error.message).to.equal('The id is required for a PassageEvent'); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('if a passage event does not have a type', function () { |
| 35 | + it('should throw an error', function () { |
| 36 | + // given |
| 37 | + class FakeEvent extends PassageEvent { |
| 38 | + constructor() { |
| 39 | + super({ id: 1 }); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // when |
| 44 | + const error = catchErrSync(() => new FakeEvent())(); |
| 45 | + |
| 46 | + // then |
| 47 | + expect(error).to.be.instanceOf(DomainError); |
| 48 | + expect(error.message).to.equal('The type is required for a PassageEvent'); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('if a passage event does not have a occurredAt', function () { |
| 53 | + it('should throw an error', function () { |
| 54 | + // given |
| 55 | + class FakeEvent extends PassageEvent { |
| 56 | + constructor() { |
| 57 | + super({ id: 1, type: 'FAKE' }); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // when |
| 62 | + const error = catchErrSync(() => new FakeEvent())(); |
| 63 | + |
| 64 | + // then |
| 65 | + expect(error).to.be.instanceOf(DomainError); |
| 66 | + expect(error.message).to.equal('The occurredAt is required for a PassageEvent'); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('if a passage event does not have a createdAt', function () { |
| 71 | + it('should throw an error', function () { |
| 72 | + // given |
| 73 | + class FakeEvent extends PassageEvent { |
| 74 | + constructor() { |
| 75 | + super({ id: 1, type: 'FAKE', occurredAt: Symbol('date') }); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // when |
| 80 | + const error = catchErrSync(() => new FakeEvent())(); |
| 81 | + |
| 82 | + // then |
| 83 | + expect(error).to.be.instanceOf(DomainError); |
| 84 | + expect(error.message).to.equal('The createdAt is required for a PassageEvent'); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('if a passage event does not have a passageId', function () { |
| 89 | + it('should throw an error', function () { |
| 90 | + // given |
| 91 | + class FakeEvent extends PassageEvent { |
| 92 | + constructor() { |
| 93 | + super({ id: 1, type: 'FAKE', occurredAt: Symbol('date'), createdAt: Symbol('date') }); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // when |
| 98 | + const error = catchErrSync(() => new FakeEvent())(); |
| 99 | + |
| 100 | + // then |
| 101 | + expect(error).to.be.instanceOf(DomainError); |
| 102 | + expect(error.message).to.equal('The passageId is required for a PassageEvent'); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('if a passage event has minimal required attributes', function () { |
| 107 | + it('should create a PassageEvent and set id attribute', function () { |
| 108 | + // given |
| 109 | + const id = Symbol('id'); |
| 110 | + const occurredAt = Symbol('date'); |
| 111 | + const createdAt = Symbol('date'); |
| 112 | + const passageId = Symbol('passage'); |
| 113 | + class FakeEvent extends PassageEvent { |
| 114 | + constructor() { |
| 115 | + super({ id, type: 'FAKE', occurredAt, createdAt, passageId }); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // when |
| 120 | + const event = new FakeEvent(); |
| 121 | + |
| 122 | + // then |
| 123 | + expect(event.id).to.equal(id); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should create a PassageEvent and set type attribute', function () { |
| 127 | + // given |
| 128 | + const id = Symbol('id'); |
| 129 | + const occurredAt = Symbol('date'); |
| 130 | + const createdAt = Symbol('date'); |
| 131 | + const passageId = Symbol('passage'); |
| 132 | + class FakeEvent extends PassageEvent { |
| 133 | + constructor() { |
| 134 | + super({ id, type: 'FAKE', occurredAt, createdAt, passageId }); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // when |
| 139 | + const event = new FakeEvent(); |
| 140 | + |
| 141 | + // then |
| 142 | + expect(event.type).to.equal('FAKE'); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should create a PassageEvent and set occurredAt attribute', function () { |
| 146 | + // given |
| 147 | + const id = Symbol('id'); |
| 148 | + const occurredAt = Symbol('date'); |
| 149 | + const createdAt = Symbol('date'); |
| 150 | + const passageId = Symbol('passage'); |
| 151 | + class FakeEvent extends PassageEvent { |
| 152 | + constructor() { |
| 153 | + super({ id, type: 'FAKE', occurredAt, createdAt, passageId }); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + // when |
| 158 | + const event = new FakeEvent(); |
| 159 | + |
| 160 | + // then |
| 161 | + expect(event.occurredAt).to.equal(occurredAt); |
| 162 | + }); |
| 163 | + |
| 164 | + it('should create a PassageEvent and set createdAt attribute', function () { |
| 165 | + // given |
| 166 | + const id = Symbol('id'); |
| 167 | + const occurredAt = Symbol('date'); |
| 168 | + const createdAt = Symbol('date'); |
| 169 | + const passageId = Symbol('passage'); |
| 170 | + class FakeEvent extends PassageEvent { |
| 171 | + constructor() { |
| 172 | + super({ id, type: 'FAKE', occurredAt, createdAt, passageId }); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + // when |
| 177 | + const event = new FakeEvent(); |
| 178 | + |
| 179 | + // then |
| 180 | + expect(event.createdAt).to.equal(createdAt); |
| 181 | + }); |
| 182 | + |
| 183 | + it('should create a PassageEvent and set passageId attribute', function () { |
| 184 | + // given |
| 185 | + const id = Symbol('id'); |
| 186 | + const occurredAt = Symbol('date'); |
| 187 | + const createdAt = Symbol('date'); |
| 188 | + const passageId = Symbol('passage'); |
| 189 | + class FakeEvent extends PassageEvent { |
| 190 | + constructor() { |
| 191 | + super({ id, type: 'FAKE', occurredAt, createdAt, passageId }); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + // when |
| 196 | + const event = new FakeEvent(); |
| 197 | + |
| 198 | + // then |
| 199 | + expect(event.passageId).to.equal(passageId); |
| 200 | + }); |
| 201 | + |
| 202 | + it('should create a PassageEvent and set data to undefined', function () { |
| 203 | + // given |
| 204 | + const id = Symbol('id'); |
| 205 | + const occurredAt = Symbol('date'); |
| 206 | + const createdAt = Symbol('date'); |
| 207 | + const passageId = Symbol('passage'); |
| 208 | + class FakeEvent extends PassageEvent { |
| 209 | + constructor() { |
| 210 | + super({ id, type: 'FAKE', occurredAt, createdAt, passageId }); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + // when |
| 215 | + const event = new FakeEvent(); |
| 216 | + |
| 217 | + // then |
| 218 | + expect(event.data).to.be.undefined; |
| 219 | + }); |
| 220 | + }); |
| 221 | + }); |
| 222 | +}); |
0 commit comments