|
| 1 | +import { beforeEach, describe, expect, test, vi } from 'vitest'; |
| 2 | +import { ContentElement } from '../../content-elements'; |
| 3 | +import { Story } from '../../mapper'; |
| 4 | + |
| 5 | +describe('Mapper', () => { |
| 6 | + const ans = { |
| 7 | + type: 'story', |
| 8 | + _id: 'arc_id', |
| 9 | + version: '1.0.0', |
| 10 | + website: 'website_id', |
| 11 | + canonical_website: 'website_id', |
| 12 | + language: 'en-GB', |
| 13 | + subtype: 'subtype', |
| 14 | + credits: { by: [] }, |
| 15 | + headlines: { basic: '' }, |
| 16 | + subheadlines: { basic: '' }, |
| 17 | + description: { basic: '' }, |
| 18 | + created_date: expect.any(String), |
| 19 | + first_publish_date: expect.any(String), |
| 20 | + publish_date: expect.any(String), |
| 21 | + display_date: expect.any(String), |
| 22 | + source: { name: 'code-store', system: 'code-store', source_id: 'source_id' }, |
| 23 | + taxonomy: { tags: [] }, |
| 24 | + workflow: expect.any(Object), |
| 25 | + content_elements: [{ type: 'text', content: '', alignment: 'left' }], |
| 26 | + additional_properties: { url: 'legacy_url' }, |
| 27 | + }; |
| 28 | + |
| 29 | + const mocks = { |
| 30 | + init: vi.fn().mockResolvedValue(undefined), |
| 31 | + getSourceId: vi.fn().mockResolvedValue('source_id'), |
| 32 | + getSourceType: vi.fn().mockResolvedValue('source_type'), |
| 33 | + getWebsiteId: vi.fn().mockResolvedValue('website_id'), |
| 34 | + getLegacyUrl: vi.fn().mockResolvedValue('legacy_url'), |
| 35 | + getArcId: vi.fn().mockResolvedValue('arc_id'), |
| 36 | + getGroupId: vi.fn().mockResolvedValue('group_id'), |
| 37 | + getSubtype: vi.fn().mockResolvedValue('subtype'), |
| 38 | + getContentElements: vi.fn().mockResolvedValue([ContentElement.text('')]), |
| 39 | + }; |
| 40 | + |
| 41 | + class CodeStory extends Story { |
| 42 | + version = () => '1.0.0' as any; |
| 43 | + groupId = mocks.getGroupId; |
| 44 | + sourceId = mocks.getSourceId; |
| 45 | + sourceType = mocks.getSourceType; |
| 46 | + websiteId = mocks.getWebsiteId; |
| 47 | + legacyUrl = mocks.getLegacyUrl; |
| 48 | + arcId = mocks.getArcId; |
| 49 | + |
| 50 | + init = mocks.init; |
| 51 | + getSubtype = mocks.getSubtype; |
| 52 | + getContentElements = mocks.getContentElements; |
| 53 | + } |
| 54 | + |
| 55 | + beforeEach(() => { |
| 56 | + vi.clearAllMocks(); |
| 57 | + }); |
| 58 | + |
| 59 | + test('Should return valid ANS', async () => { |
| 60 | + const story = new CodeStory(); |
| 61 | + |
| 62 | + const ans = await story.getAns(); |
| 63 | + |
| 64 | + expect(ans).toBeDefined(); |
| 65 | + expect(ans.type).toBe('story'); |
| 66 | + expect(ans._id).toBe('arc_id'); |
| 67 | + expect(ans.version).toBe('1.0.0'); |
| 68 | + }); |
| 69 | + |
| 70 | + test('prepare() should call params, payload, getAns', async () => { |
| 71 | + const story = new CodeStory(); |
| 72 | + const getAns = vi.spyOn(story, 'getAns'); |
| 73 | + const payload = vi.spyOn(story, 'payload' as any); |
| 74 | + const params = vi.spyOn(story, 'params' as any); |
| 75 | + const result = await story.prepare(); |
| 76 | + |
| 77 | + expect(mocks.init).toBeCalledTimes(1); |
| 78 | + expect(getAns).toBeCalledTimes(1); |
| 79 | + expect(payload).toBeCalledTimes(1); |
| 80 | + expect(params).toBeCalledTimes(1); |
| 81 | + expect(mocks.getContentElements).toBeCalledTimes(1); |
| 82 | + |
| 83 | + expect(result.params).toBeDefined(); |
| 84 | + expect(result.params.website).toBe('website_id'); |
| 85 | + expect(result.params.groupId).toBe('group_id'); |
| 86 | + expect(result.params.priority).toBe('historical'); |
| 87 | + |
| 88 | + expect(result.payload).toBeDefined(); |
| 89 | + expect(result.payload.sourceId).toBe('source_id'); |
| 90 | + expect(result.payload.sourceType).toBe('source_type'); |
| 91 | + expect(result.payload.ANS).toMatchObject(ans); |
| 92 | + }); |
| 93 | +}); |
0 commit comments