|
| 1 | +import { ApiConfig, submitContactInfo, ContactDTO, WriteError, Contact } from '../../../src' |
| 2 | +import { TestConstants } from '../../testHelpers/TestConstants' |
| 3 | +import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' |
| 4 | + |
| 5 | +describe('submitContactInfo', () => { |
| 6 | + beforeAll(async () => { |
| 7 | + ApiConfig.init( |
| 8 | + TestConstants.TEST_API_URL, |
| 9 | + DataverseApiAuthMechanism.API_KEY, |
| 10 | + process.env.TEST_API_KEY |
| 11 | + ) |
| 12 | + }) |
| 13 | + |
| 14 | + test('should return Contact result successfully with targetId', async () => { |
| 15 | + const contactDTO: ContactDTO = { |
| 16 | + targetId: 1, |
| 17 | + subject: 'Data Question', |
| 18 | + body: 'Please help me understand your data. Thank you!', |
| 19 | + |
| 20 | + } |
| 21 | + |
| 22 | + let contactInfo: Contact[] = [] |
| 23 | + try { |
| 24 | + contactInfo = await submitContactInfo.execute(contactDTO) |
| 25 | + } catch (error) { |
| 26 | + throw new Error('Contact info should be submitted') |
| 27 | + } finally { |
| 28 | + expect(contactInfo).toBeDefined() |
| 29 | + expect(contactInfo[0].fromEmail).toEqual('[email protected]') |
| 30 | + expect(contactInfo[0].subject).toEqual(expect.any(String)) |
| 31 | + expect(contactInfo[0].body).toEqual(expect.any(String)) |
| 32 | + } |
| 33 | + }) |
| 34 | + |
| 35 | + test('should return a Contact when targetId is not provided', async () => { |
| 36 | + const contactDTO: ContactDTO = { |
| 37 | + subject: 'General Inquiry', |
| 38 | + body: 'I have a general question.', |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + let contactInfo: Contact[] = [] |
| 43 | + try { |
| 44 | + contactInfo = await submitContactInfo.execute(contactDTO) |
| 45 | + } catch (error) { |
| 46 | + throw new Error('Contact info should be submitted even if target id is missing') |
| 47 | + } finally { |
| 48 | + expect(contactInfo).toBeDefined() |
| 49 | + expect(contactInfo[0].fromEmail).toEqual('[email protected]') |
| 50 | + expect(contactInfo[0].subject).toEqual(expect.any(String)) |
| 51 | + expect(contactInfo[0].body).toEqual(expect.any(String)) |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + test('should return Contact when contact info is successfully submitted with identifier', async () => { |
| 56 | + const test2ContactDTO: ContactDTO = { |
| 57 | + identifier: 'root', |
| 58 | + subject: 'Data Question', |
| 59 | + body: 'Please help me understand your data. Thank you!', |
| 60 | + |
| 61 | + } |
| 62 | + const contactInfo = await submitContactInfo.execute(test2ContactDTO) |
| 63 | + |
| 64 | + expect(contactInfo).toBeDefined() |
| 65 | + expect(contactInfo[0].fromEmail).toEqual(test2ContactDTO.fromEmail) |
| 66 | + expect(contactInfo[0].subject).toEqual(expect.any(String)) |
| 67 | + expect(contactInfo[0].body).toEqual(expect.any(String)) |
| 68 | + }) |
| 69 | + |
| 70 | + test('should return error if the target id is unexisted', async () => { |
| 71 | + const contactDTO: ContactDTO = { |
| 72 | + targetId: 0, |
| 73 | + subject: '', |
| 74 | + body: '', |
| 75 | + |
| 76 | + } |
| 77 | + const expectedError = new WriteError(`[400] Feedback target object not found.`) |
| 78 | + await expect(submitContactInfo.execute(contactDTO)).rejects.toThrow(expectedError) |
| 79 | + }) |
| 80 | +}) |
0 commit comments