11import * as admin from "firebase-admin" ;
2- import { firestore } from "firebase-admin" ;
32import { smtpServer } from "./createSMTPServer" ;
43
5- // import wait-for-expect
6- import waitForExpect from "wait-for-expect" ;
7-
84process . env . FIRESTORE_EMULATOR_HOST = "127.0.0.1:8080" ;
95
106admin . initializeApp ( {
@@ -14,6 +10,9 @@ admin.initializeApp({
1410const mail = "mail" ;
1511const mailCollection = admin . firestore ( ) . collection ( mail ) ;
1612
13+ const mailSg = "mail-sg" ;
14+ const mailSgCollection = admin . firestore ( ) . collection ( mailSg ) ;
15+
1716const templates = "templates" ;
1817const templatesCollection = admin . firestore ( ) . collection ( templates ) ;
1918
@@ -32,27 +31,24 @@ describe("e2e testing", () => {
3231 } ,
3332 } ;
3433
35- const doc = mailCollection . doc ( ) ;
36-
37- let currentSnapshot : firestore . DocumentSnapshot ;
38-
39- const unsubscribe = doc . onSnapshot ( ( snapshot ) => {
40- currentSnapshot = snapshot ;
41- } ) ;
34+ const doc = await mailCollection . add ( record ) ;
4235
43- await doc . create ( record ) ;
44-
45- await waitForExpect ( ( ) => {
46- expect ( currentSnapshot ) . toHaveProperty ( "exists" ) ;
47- expect ( currentSnapshot . exists ) . toBeTruthy ( ) ;
48- const currentDocumentData = currentSnapshot . data ( ) ;
49- expect ( currentDocumentData ) . toHaveProperty ( "delivery" ) ;
50- expect ( currentDocumentData . delivery ) . toHaveProperty ( "info" ) ;
51- expect ( currentDocumentData . delivery . info . accepted [ 0 ] ) . toEqual ( record . to ) ;
52- expect ( currentDocumentData . delivery . info . response ) . toContain ( "250" ) ;
53- unsubscribe ( ) ;
36+ return new Promise ( ( resolve , reject ) => {
37+ const unsubscribe = doc . onSnapshot ( async ( snapshot ) => {
38+ const currentDocumentData = snapshot . data ( ) ;
39+ if ( currentDocumentData . delivery && currentDocumentData . delivery . info ) {
40+ expect ( currentDocumentData ) . toHaveProperty ( "delivery" ) ;
41+ expect ( currentDocumentData . delivery ) . toHaveProperty ( "info" ) ;
42+ expect ( currentDocumentData . delivery . info . accepted [ 0 ] ) . toEqual (
43+ record . to
44+ ) ;
45+ expect ( currentDocumentData . delivery . info . response ) . toContain ( "250" ) ;
46+ unsubscribe ( ) ;
47+ resolve ( ) ;
48+ }
49+ } ) ;
5450 } ) ;
55- } , 12000 ) ;
51+ } ) ;
5652
5753 test ( "the expireAt field should be added, with value 5 days later than startTime" , async ( ) : Promise < void > => {
5854 const record = {
@@ -65,23 +61,22 @@ describe("e2e testing", () => {
6561 const doc = await mailCollection . add ( record ) ;
6662
6763 return new Promise ( ( resolve , reject ) => {
68- const unsubscribe = doc . onSnapshot ( ( snapshot ) => {
69- const document = snapshot . data ( ) ;
70-
64+ const unsubscribe = doc . onSnapshot ( async ( snapshot ) => {
65+ const currentDocumentData = snapshot . data ( ) ;
7166 if (
72- document . delivery &&
73- document . delivery . info &&
74- document . delivery . expireAt
67+ currentDocumentData . delivery &&
68+ currentDocumentData . delivery . info &&
69+ currentDocumentData . delivery . expireAt
7570 ) {
76- const startAt = document . delivery . startTime . toDate ( ) ;
77- const expireAt = document . delivery . expireAt . toDate ( ) ;
71+ const startAt = currentDocumentData . delivery . startTime . toDate ( ) ;
72+ const expireAt = currentDocumentData . delivery . expireAt . toDate ( ) ;
7873 expect ( expireAt . getTime ( ) - startAt . getTime ( ) ) . toEqual ( 5 * 86400000 ) ;
7974 unsubscribe ( ) ;
8075 resolve ( ) ;
8176 }
8277 } ) ;
8378 } ) ;
84- } , 12000 ) ;
79+ } ) ;
8580
8681 test ( "empty template attachments should default to message attachments" , async ( ) : Promise < void > => {
8782 //create template
@@ -100,11 +95,13 @@ describe("e2e testing", () => {
10095
10196 const doc = await mailCollection . add ( record ) ;
10297
103- return new Promise ( ( resolve , reject ) => {
104- const unsubscribe = doc . onSnapshot ( ( snapshot ) => {
98+ return new Promise ( ( resolve ) => {
99+ const unsubscribe = doc . onSnapshot ( async ( snapshot ) => {
105100 const document = snapshot . data ( ) ;
106101
107102 if ( document . delivery && document . delivery . info ) {
103+ const startAt = document . delivery . startTime . toDate ( ) ;
104+ const expireAt = document . delivery . expireAt . toDate ( ) ;
108105 expect ( document . delivery . info . accepted [ 0 ] ) . toEqual ( record . to ) ;
109106 expect ( document . delivery . info . response ) . toContain ( "250 Accepted" ) ;
110107 expect ( document . message . attachments . length ) . toEqual ( 1 ) ;
@@ -113,7 +110,7 @@ describe("e2e testing", () => {
113110 }
114111 } ) ;
115112 } ) ;
116- } , 8000 ) ;
113+ } ) ;
117114
118115 test ( "should successfully send an email with a basic template" , async ( ) : Promise < void > => {
119116 /** create basic template */
@@ -135,17 +132,18 @@ describe("e2e testing", () => {
135132 /** Add a new mail document */
136133 const doc = await mailCollection . add ( record ) ;
137134
138- /** Check the email response */
139- return new Promise ( ( resolve , reject ) => {
140- const unsubscribe = doc . onSnapshot ( ( snapshot ) => {
135+ return new Promise ( ( resolve ) => {
136+ const unsubscribe = doc . onSnapshot ( async ( snapshot ) => {
141137 const document = snapshot . data ( ) ;
142138
143139 if ( document . delivery && document . delivery . info ) {
144- expect ( document . delivery . info . accepted [ 0 ] ) . toEqual ( record . to ) ;
145- expect ( document . delivery . info . response ) . toContain ( "250 Accepted" ) ;
140+ if ( document . delivery && document . delivery . info ) {
141+ expect ( document . delivery . info . accepted [ 0 ] ) . toEqual ( record . to ) ;
142+ expect ( document . delivery . info . response ) . toContain ( "250 Accepted" ) ;
146143
147- unsubscribe ( ) ;
148- resolve ( ) ;
144+ unsubscribe ( ) ;
145+ resolve ( ) ;
146+ }
149147 }
150148 } ) ;
151149 } ) ;
@@ -165,25 +163,21 @@ describe("e2e testing", () => {
165163 } ,
166164 } ;
167165
168- let currentSnapshot : firestore . DocumentSnapshot ;
169-
170- /** Add a new mail document */
171- const doc = await firestore ( ) . collection ( "mail-sg" ) . add ( record ) ;
172-
173- const unsubscribe = doc . onSnapshot ( ( snapshot ) => {
174- currentSnapshot = snapshot ;
175- } ) ;
176-
177- await waitForExpect ( ( ) => {
178- const document = currentSnapshot . data ( ) ;
179- expect ( document . delivery . state ) . toEqual ( "SUCCESS" ) ;
166+ const doc = await mailSgCollection . add ( record ) ;
180167
181- unsubscribe ( ) ;
168+ return new Promise ( ( resolve ) => {
169+ const unsubscribe = doc . onSnapshot ( ( snapshot ) => {
170+ const document = snapshot . data ( ) ;
171+ if ( document . delivery && document . delivery . info ) {
172+ expect ( document . delivery . state ) . toEqual ( "SUCCESS" ) ;
173+ unsubscribe ( ) ;
174+ resolve ( ) ;
175+ }
176+ } ) ;
182177 } ) ;
183- } ) ;
178+ } , 12000 ) ;
184179
185180 test ( "should error when sending an email with an empty SendGrid template" , async ( ) : Promise < void > => {
186- /** Add a record with the template and no message object */
187181 const record = {
188182189183 sendGrid : {
@@ -195,26 +189,23 @@ describe("e2e testing", () => {
195189 } ,
196190 } ;
197191
198- /** Add a new mail document */
199- const doc = await firestore ( ) . collection ( "mail-sg" ) . add ( record ) ;
200-
201- let currentSnapshot : firestore . DocumentSnapshot ;
202-
203- const unsubscribe = doc . onSnapshot ( ( snapshot ) => {
204- currentSnapshot = snapshot ;
205- } ) ;
206-
207- await waitForExpect ( ( ) => {
208- const document = currentSnapshot . data ( ) ;
192+ const doc = await mailSgCollection . add ( record ) ;
209193
210- expect ( document . delivery . state ) . toEqual ( "ERROR" ) ;
211- expect ( document . delivery . error ) . toEqual (
212- "Error: SendGrid templateId is not provided, if you're using SendGrid Dynamic Templates, please provide a valid templateId, otherwise provide a `text` or `html` content."
213- ) ;
194+ return new Promise ( ( resolve ) => {
195+ const unsubscribe = doc . onSnapshot ( async ( snapshot ) => {
196+ const document = snapshot . data ( ) ;
214197
215- unsubscribe ( ) ;
198+ if ( document . delivery && document . delivery . error ) {
199+ expect ( document . delivery . state ) . toEqual ( "ERROR" ) ;
200+ expect ( document . delivery . error ) . toEqual (
201+ `Error: SendGrid templateId is not provided, if you're using SendGrid Dynamic Templates, please provide a valid templateId, otherwise provide a \`text\` or \`html\` content.`
202+ ) ;
203+ unsubscribe ( ) ;
204+ resolve ( ) ;
205+ }
206+ } ) ;
216207 } ) ;
217- } ) ;
208+ } , 12000 ) ;
218209
219210 afterAll ( ( ) => {
220211 server . close ( ) ;
0 commit comments