1+ import type { Express } from "express" ;
12import request from "supertest" ;
2- import type { Answers_v1 } from "@core/types/waitlist/waitlist.answer.types" ;
3+ import type {
4+ Answers_v1 ,
5+ Answers_v2 ,
6+ } from "@core/types/waitlist/waitlist.answer.types" ;
37
48describe ( "POST /api/waitlist" , ( ) => {
5- beforeEach ( ( ) => jest . resetModules ( ) ) ;
6- it ( "should return 400 if answers are invalid" , async ( ) => {
7- jest . doMock ( "../service/waitlist.service" , ( ) => ( {
8- __esModule : true ,
9- default : {
10- addToWaitlist : jest . fn ( ) ,
11- } ,
12- } ) ) ;
9+ let app : Express ;
10+ let mockAddToWaitlist : jest . Mock ;
11+
12+ const createTestApp = async ( mocks ?: {
13+ env ?: Record < string , unknown > ;
14+ service ?: Record < string , unknown > ;
15+ } ) => {
16+ if ( mocks ?. env ) {
17+ jest . doMock ( "@backend/common/constants/env.constants" , ( ) => mocks . env ) ;
18+ }
19+ if ( mocks ?. service ) {
20+ jest . doMock ( "../service/waitlist.service" , ( ) => mocks . service ) ;
21+ }
22+
1323 const { WaitlistController } = await import ( "./waitlist.controller" ) ;
1424 const express = ( await import ( "express" ) ) . default ;
15- const app = express ( ) ;
16- app . use ( express . json ( ) ) ;
17- app . post ( "/api/waitlist" , WaitlistController . addToWaitlist ) ;
25+ const testApp = express ( ) ;
26+ testApp . use ( express . json ( ) ) ;
27+ testApp . post ( "/api/waitlist" , WaitlistController . addToWaitlist ) ;
28+ return testApp ;
29+ } ;
30+
31+ beforeEach ( ( ) => {
32+ jest . resetModules ( ) ;
33+ mockAddToWaitlist = jest . fn ( ) ;
34+ } ) ;
35+
36+ it ( "should return 400 if answers are invalid" , async ( ) => {
37+ app = await createTestApp ( {
38+ service : {
39+ __esModule : true ,
40+ default : { addToWaitlist : mockAddToWaitlist } ,
41+ } ,
42+ } ) ;
1843
1944 const res = await request ( app )
2045 . post ( "/api/waitlist" )
@@ -24,18 +49,14 @@ describe("POST /api/waitlist", () => {
2449 expect ( res . error ) . toBeDefined ( ) ;
2550 } ) ;
2651
27- it ( "should return 200 if answers are valid" , async ( ) => {
28- jest . doMock ( "../service/waitlist.service" , ( ) => ( {
29- __esModule : true ,
30- default : {
31- addToWaitlist : jest . fn ( ) . mockResolvedValue ( undefined ) ,
52+ it ( "should return 200 if v1 answers are valid" , async ( ) => {
53+ mockAddToWaitlist . mockResolvedValue ( undefined ) ;
54+ app = await createTestApp ( {
55+ service : {
56+ __esModule : true ,
57+ default : { addToWaitlist : mockAddToWaitlist } ,
3258 } ,
33- } ) ) ;
34- const { WaitlistController } = await import ( "./waitlist.controller" ) ;
35- const express = ( await import ( "express" ) ) . default ;
36- const app = express ( ) ;
37- app . use ( express . json ( ) ) ;
38- app . post ( "/api/waitlist" , WaitlistController . addToWaitlist ) ;
59+ } ) ;
3960
4061 const answers : Answers_v1 = {
4162 email : "test@example.com" ,
@@ -47,22 +68,43 @@ describe("POST /api/waitlist", () => {
4768 currentlyPayingFor : [ ] ,
4869 anythingElse : "I'm a test" ,
4970 } ;
71+
72+ const res = await request ( app ) . post ( "/api/waitlist" ) . send ( answers ) ;
73+
74+ expect ( res . status ) . toBe ( 200 ) ;
75+ expect ( res . body . error ) . not . toBeDefined ( ) ;
76+ expect ( mockAddToWaitlist ) . toHaveBeenCalledWith ( answers . email , answers ) ;
77+ } ) ;
78+
79+ it ( "should return 200 if v2 answers are valid" , async ( ) => {
80+ mockAddToWaitlist . mockResolvedValue ( undefined ) ;
81+ app = await createTestApp ( {
82+ service : {
83+ __esModule : true ,
84+ default : { addToWaitlist : mockAddToWaitlist } ,
85+ } ,
86+ } ) ;
87+
88+ const answers : Answers_v2 = {
89+ email : "test@example.com" ,
90+ schemaVersion : "2" ,
91+ } ;
92+
5093 const res = await request ( app ) . post ( "/api/waitlist" ) . send ( answers ) ;
5194
5295 expect ( res . status ) . toBe ( 200 ) ;
5396 expect ( res . body . error ) . not . toBeDefined ( ) ;
97+ expect ( mockAddToWaitlist ) . toHaveBeenCalledWith ( answers . email , answers ) ;
5498 } ) ;
55- // this test is at the bottom to avoid
56- // having to reset ENV in each test
99+
57100 it ( "should return 500 if emailer values are missing" , async ( ) => {
58- jest . doMock ( "@backend/common/constants/env.constants" , ( ) => ( {
59- ENV : { } ,
60- } ) ) ;
61- const { WaitlistController } = await import ( "./waitlist.controller" ) ;
62- const express = ( await import ( "express" ) ) . default ;
63- const app = express ( ) ;
64- app . use ( express . json ( ) ) ;
65- app . post ( "/api/waitlist" , WaitlistController . addToWaitlist ) ;
101+ app = await createTestApp ( {
102+ env : { ENV : { } } ,
103+ service : {
104+ __esModule : true ,
105+ default : { addToWaitlist : mockAddToWaitlist } ,
106+ } ,
107+ } ) ;
66108
67109 const answers : Answers_v1 = {
68110 email : "test@example.com" ,
@@ -73,6 +115,7 @@ describe("POST /api/waitlist", () => {
73115 currentlyPayingFor : [ ] ,
74116 profession : "Founder" ,
75117 } ;
118+
76119 const res = await request ( app ) . post ( "/api/waitlist" ) . send ( answers ) ;
77120 expect ( res . status ) . toBe ( 500 ) ;
78121 expect ( res . body . error ) . toBe ( "Missing emailer value(s)" ) ;
0 commit comments