|
| 1 | +import { describe, expect, test, beforeEach } from "bun:test"; |
| 2 | +import { EquipmentManufacturer } from "@models/EquipmentManufacturer"; |
| 3 | +import { JsonApiSerializer } from '../../src/utils/JsonSerializer'; |
| 4 | +import { Hydrator } from '../../src/utils/Hydrator'; |
| 5 | + |
| 6 | +describe('EquipmentManufacturer Model', () => { |
| 7 | + const newId = 'b9cbbac7-65aa-40d1-aed8-f68b71aa6b6e'; |
| 8 | + const newManufacturerData = { |
| 9 | + name: 'Test Manufacturer' |
| 10 | + }; |
| 11 | + |
| 12 | + let newManufacturer; |
| 13 | + let serializer; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + newManufacturer = new EquipmentManufacturer(newManufacturerData); |
| 17 | + const hydrator = new Hydrator(); |
| 18 | + serializer = new JsonApiSerializer(hydrator.getModelMap()); |
| 19 | + }); |
| 20 | + |
| 21 | + const verifyPayloadStructure = (payload, includeId = false) => { |
| 22 | + const expectedPayload = { |
| 23 | + data: { |
| 24 | + type: "equipment-manufacturers", |
| 25 | + attributes: { |
| 26 | + name: newManufacturerData.name |
| 27 | + }, |
| 28 | + relationships: {} |
| 29 | + } |
| 30 | + }; |
| 31 | + |
| 32 | + if (includeId) { |
| 33 | + expectedPayload.data['id'] = newId; |
| 34 | + } |
| 35 | + |
| 36 | + expect(payload).toEqual(expectedPayload); |
| 37 | + }; |
| 38 | + |
| 39 | + test('newly created model should have correct attributes', () => { |
| 40 | + expect(newManufacturer.type).toBe('equipment-manufacturers'); |
| 41 | + expect(newManufacturer.name).toBe(newManufacturerData.name); |
| 42 | + }); |
| 43 | + |
| 44 | + test('create payload should have correct attributes and relationships', () => { |
| 45 | + const payload = serializer.buildCreatePayload(newManufacturer); |
| 46 | + verifyPayloadStructure(payload); |
| 47 | + }); |
| 48 | + |
| 49 | + test('patch payload should have correct attributes and relationships', () => { |
| 50 | + newManufacturer.id = newId; |
| 51 | + const payload = serializer.buildUpdatePayload(newManufacturer); |
| 52 | + verifyPayloadStructure(payload, true); |
| 53 | + }); |
| 54 | +}); |
0 commit comments