UNPKG

@websolutespa/payload-plugin-bowl

Version:

Bowl PayloadCms plugin of the BOM Repository

218 lines (217 loc) 9.27 kB
import { config, NewsletterAction, RegisterAction } from '@/test'; import { clearContext, endUsers, getContext, users } from '@websolutespa/test/payload'; import { afterAll, beforeAll, describe, expect, it } from 'vitest'; import { options } from '../../options'; let newsletterActionDoc; let registerActionDoc; let endUser; const newsletterConsent = 'newsletter'; const registerConsent = 'register'; const endUserData = { email: 'foo@bar.com', firstName: 'foo', lastName: 'bar' }; describe('action.service', ()=>{ let payload; let client; let userToken; let endUserToken; let apiKey; beforeAll(async ()=>{ const context = await getContext(config); payload = context.payload; client = context.client; userToken = await client.getToken('users', users.admin); endUserToken = await client.getToken('end_users', endUsers.user); apiKey = users.api.apiKey; }); afterAll(async ()=>{ await clearContext(); }); it('ready', async ()=>{ expect(payload).toBeDefined(); expect(client).toBeDefined(); expect(userToken).toBeDefined(); expect(endUserToken).toBeDefined(); }); describe('optin', ()=>{ beforeAll(async ()=>{ // setup consent preferences await payload.create({ collection: options.slug.consentPreference, data: { id: newsletterConsent } }); await payload.create({ collection: options.slug.consentPreference, data: { id: registerConsent } }); }); describe('newsletter_action create', ()=>{ beforeAll(async ()=>{ ({ doc: newsletterActionDoc } = await client.post(NewsletterAction.slug, { consentPreferences: [ newsletterConsent ], ...endUserData }, { headers: { Authorization: `users API-Key ${users.api.apiKey}` } })); }); it('check custom createEndUser option', async ()=>{ const response = await payload.find({ collection: options.slug.endUsers, where: { email: { equals: endUserData.email } } }); if (typeof NewsletterAction.custom?.createEndUser === 'string') { expect(response.totalDocs).toEqual(1); expect(response.docs[0].roles).toEqual([ NewsletterAction.custom?.createEndUser ]); } else { expect(response.totalDocs).toEqual(0); } }); describe('optin newsletter', ()=>{ it('optin should be successful', async ()=>{ const data = await client.get(`/actions/optin/${newsletterActionDoc.id}/${NewsletterAction.slug}`); expect(data.status).toEqual(200); }); describe('endUser', ()=>{ it('should have been created', async ()=>{ const { docs: endUsers } = await payload.find({ collection: options.slug.endUsers, where: { email: { equals: endUserData.email } } }); endUser = endUsers[0]; expect(endUser).toBeDefined(); }); it('should have empty fields (fields were not flagged as "updateEndUser")', async ()=>{ console.log(endUser); expect(endUser.firstName).toEqual('-'); expect(endUser.lastName).toEqual('-'); }); it('should have the newsletter consent assigned', async ()=>{ expect(endUser.consentPreferences.map((x)=>x.consentPreference.id)).toContain(newsletterConsent); }); }); }); }); describe('register_action create', ()=>{ beforeAll(async ()=>{ ({ doc: registerActionDoc } = await client.post(RegisterAction.slug, { ...endUserData, consentPreferences: [ registerConsent ], password: 'FooBarPassword1!' }, { headers: { Authorization: `users API-Key ${users.api.apiKey}` } })); }); describe('optin register', ()=>{ it('optin should be successful', async ()=>{ const data = await client.get(`/actions/optin/${registerActionDoc.id}/${RegisterAction.slug}`); expect(data.status).toEqual(200); }); describe('endUser', ()=>{ beforeAll(async ()=>{ // reload the updated endUser doc const { docs: endUsers } = await payload.find({ collection: options.slug.endUsers, where: { email: { equals: endUserData.email } } }); endUser = endUsers[0]; }); it('should have been updated (fields were flagged as "updateEndUser")', async ()=>{ expect(endUser.firstName).toEqual(registerActionDoc.firstName); expect(endUser.lastName).toEqual(registerActionDoc.lastName); }); it('should have the register consent assigned', async ()=>{ expect(endUser.consentPreferences.map((x)=>x.consentPreference.id)).toContain(registerConsent); }); it('should have been created', async ()=>{ const response = await payload.find({ collection: options.slug.endUsers, where: { email: { equals: endUserData.email } } }); expect(response.totalDocs).toEqual(1); }); }); }); }); }); describe('optout', ()=>{ describe('optout newsletter', ()=>{ it('optout should be successful', async ()=>{ const data = await client.get(`/actions/optout/${newsletterActionDoc.id}/${NewsletterAction.slug}`); expect(data.status).toEqual(200); }); describe('endUser', ()=>{ beforeAll(async ()=>{ // reload the updated endUser doc const { docs: endUsers } = await payload.find({ collection: options.slug.endUsers, where: { email: { equals: endUserData.email } } }); // console.log('endUser', endUser); endUser = endUsers[0]; }); it('should have the newsletter consent removed', async ()=>{ expect(endUser.consentPreferences.map((x)=>x.consentPreference.id)).not.toContain(newsletterConsent); }); it('should still have the register consent assigned', async ()=>{ expect(endUser.consentPreferences.map((x)=>x.consentPreference.id)).toContain(registerConsent); }); }); describe('original newsletter action', ()=>{ beforeAll(async ()=>{ const response = await payload.find({ collection: NewsletterAction.slug, where: { id: { equals: newsletterActionDoc.id } } }); newsletterActionDoc = response.docs[0]; }); it('should have the consentRevoked field set to true', async ()=>{ expect(newsletterActionDoc.consentsRevoked).toEqual(true); }); it('should have the consentRevokedDate field set', async ()=>{ expect(newsletterActionDoc.consentsRevokedDate).toBeDefined(); }); }); }); }); }); //# sourceMappingURL=action.service.test.js.map