@@ -4,8 +4,8 @@ import request from 'supertest'
44
55import { CardActions as SheetCardActions } from '../../src/constants/card-actions'
66import { GoogleSheetsService } from '../../src/services/google-sheets.service'
7- import { UserDatabaseService } from '../../src/services/user-database.service'
87import { buildUser } from '../fixtures'
8+ import { setupGetToken , setupGetUser } from '../setups'
99
1010import { createTestApp } from './helpers'
1111
@@ -23,12 +23,9 @@ describe('export e2e tests', () => {
2323 } )
2424
2525 it ( 'returns the no tasks card if no tasks for the specified project' , ( ) => {
26- jest . spyOn ( UserDatabaseService . prototype , 'getUser' ) . mockImplementation ( ( ) =>
27- Promise . resolve ( buildUser ( ) ) ,
28- )
29- jest . spyOn ( GoogleSheetsService . prototype , 'getCurrentOrRefreshedToken' ) . mockImplementation (
30- ( ) => Promise . resolve ( { token : 'token' , userId : '42' } ) ,
31- )
26+ setupGetUser ( buildUser ( ) )
27+ setupGetToken ( 'token' )
28+
3229 jest . spyOn ( TodoistApi . prototype , 'getTasks' ) . mockImplementation ( ( ) => Promise . resolve ( [ ] ) )
3330
3431 return request ( app . getHttpServer ( ) )
@@ -45,6 +42,9 @@ describe('export e2e tests', () => {
4542 content : 'My Project' ,
4643 contentPlain : 'My Project' ,
4744 } ,
45+ inputs : {
46+ 'Input.completed' : 'true' ,
47+ } ,
4848 } ,
4949 } )
5050 . expect ( 200 )
@@ -55,4 +55,114 @@ describe('export e2e tests', () => {
5555 expect ( JSON . stringify ( body ) ) . toMatch ( / \* \* M y P r o j e c t \* \* h a s n o t a s k s t o e x p o r t / )
5656 } )
5757 } )
58+
59+ it ( 'returns the error card if inputs are not present (should not happen, but you never know)' , ( ) => {
60+ setupGetUser ( buildUser ( ) )
61+ setupGetToken ( 'token' )
62+
63+ return request ( app . getHttpServer ( ) )
64+ . post ( '/process' )
65+ . send ( {
66+ context : { user : { id : 42 } , theme : 'light' } ,
67+ action : {
68+ actionType : 'submit' ,
69+ actionId : SheetCardActions . Export ,
70+ params : {
71+ source : 'project' ,
72+ sourceId : 1234 ,
73+ url : 'https://google.com' ,
74+ content : 'My Project' ,
75+ contentPlain : 'My Project' ,
76+ } ,
77+ } ,
78+ } )
79+ . expect ( 400 )
80+ . expect ( 'Content-Type' , / j s o n / )
81+ . then ( ( response ) => {
82+ const body = response . body as DoistCardResponse
83+ expect ( body . card ) . toBeDefined ( )
84+
85+ const json = JSON . stringify ( body )
86+ expect ( json ) . toMatch ( / U n f o r t u n a t e l y , i t l o o k s l i k e s o m e t h i n g w e n t w r o n g ./ )
87+ expect ( json ) . not . toMatch ( / R e t r y / )
88+ } )
89+ } )
90+
91+ it ( 'returns the error card if talking to Todoist fails' , ( ) => {
92+ setupGetUser ( buildUser ( ) )
93+ setupGetToken ( 'token' )
94+
95+ jest . spyOn ( TodoistApi . prototype , 'getTasks' ) . mockImplementation ( ( ) => {
96+ throw new Error ( 'Error talking to Todoist' )
97+ } )
98+
99+ return request ( app . getHttpServer ( ) )
100+ . post ( '/process' )
101+ . send ( {
102+ context : { user : { id : 42 } , theme : 'light' } ,
103+ action : {
104+ actionType : 'submit' ,
105+ actionId : SheetCardActions . Export ,
106+ params : {
107+ source : 'project' ,
108+ sourceId : 1234 ,
109+ url : 'https://google.com' ,
110+ content : 'My Project' ,
111+ contentPlain : 'My Project' ,
112+ } ,
113+ inputs : {
114+ 'Input.completed' : 'true' ,
115+ } ,
116+ } ,
117+ } )
118+ . expect ( 500 )
119+ . expect ( 'Content-Type' , / j s o n / )
120+ . then ( ( response ) => {
121+ const body = response . body as DoistCardResponse
122+ expect ( body . card ) . toBeDefined ( )
123+
124+ const json = JSON . stringify ( body )
125+ expect ( json ) . toMatch ( / U n f o r t u n a t e l y , i t l o o k s l i k e s o m e t h i n g w e n t w r o n g ./ )
126+ expect ( json ) . toMatch ( / R e t r y / )
127+ } )
128+ } )
129+
130+ it ( 'returns the error card if talking to Google fails' , ( ) => {
131+ setupGetUser ( buildUser ( ) )
132+ setupGetToken ( 'token' )
133+
134+ jest . spyOn ( GoogleSheetsService . prototype , 'exportToSheets' ) . mockImplementation ( ( ) => {
135+ throw new Error ( 'Generic error talking to Google' )
136+ } )
137+
138+ return request ( app . getHttpServer ( ) )
139+ . post ( '/process' )
140+ . send ( {
141+ context : { user : { id : 42 } , theme : 'light' } ,
142+ action : {
143+ actionType : 'submit' ,
144+ actionId : SheetCardActions . Export ,
145+ params : {
146+ source : 'project' ,
147+ sourceId : 1234 ,
148+ url : 'https://google.com' ,
149+ content : 'My Project' ,
150+ contentPlain : 'My Project' ,
151+ } ,
152+ inputs : {
153+ 'Input.completed' : 'true' ,
154+ } ,
155+ } ,
156+ } )
157+ . expect ( 500 )
158+ . expect ( 'Content-Type' , / j s o n / )
159+ . then ( ( response ) => {
160+ const body = response . body as DoistCardResponse
161+ expect ( body . card ) . toBeDefined ( )
162+
163+ const json = JSON . stringify ( body )
164+ expect ( json ) . toMatch ( / U n f o r t u n a t e l y , i t l o o k s l i k e s o m e t h i n g w e n t w r o n g ./ )
165+ expect ( json ) . toMatch ( / R e t r y / )
166+ } )
167+ } )
58168} )
0 commit comments