@@ -5,6 +5,11 @@ describe("utils", () => {
55 let mockParams : Params ;
66 const mockKey = "pupper" ;
77 const mockValue = "nessa" ;
8+ const mockDataToken = "random+data_token" ;
9+ const mockFullscriptOptions = {
10+ publicKey : "mockPublicKey" ,
11+ env : "us" ,
12+ } ;
813
914 beforeEach ( ( ) => {
1015 elem = document . createElement ( "div" ) ;
@@ -32,28 +37,66 @@ describe("utils", () => {
3237 } ) ;
3338
3439 describe ( "buildQueryString" , ( ) => {
35- it ( "returns an empty string if params have no keys" , ( ) => {
40+ it ( "returns an empty string if params have no keys" , async ( ) => {
3641 mockParams = { } ;
37- const builtQueryString = buildQueryString ( mockParams ) ;
38- expect ( builtQueryString ) . toHaveLength ( 0 ) ;
42+ const builtQueryString = await buildQueryString ( mockParams ) ;
43+ await expect ( builtQueryString ) . toHaveLength ( 0 ) ;
3944 } ) ;
4045
41- it ( "properly builds a query string with proper key and param values" , ( ) => {
42- const builtQueryString = buildQueryString ( mockParams ) ;
46+ it ( "properly builds a query string with proper key and param values" , async ( ) => {
47+ const builtQueryString = await buildQueryString ( mockParams ) ;
4348
44- expect ( builtQueryString ) . toEqual ( `?key=${ mockKey } &value=${ mockValue } ` ) ;
49+ await expect ( builtQueryString ) . toEqual ( `?key=${ mockKey } &value=${ mockValue } ` ) ;
4550 } ) ;
4651
47- it ( "converts keys into snakecase" , ( ) => {
52+ it ( "converts keys into snakecase" , async ( ) => {
4853 mockParams = { fooBar : "foobar" } ;
49- const builtQueryString = buildQueryString ( mockParams ) ;
50- expect ( builtQueryString ) . toEqual ( `?foo_bar=${ mockParams . fooBar } ` ) ;
54+ const builtQueryString = await buildQueryString ( mockParams ) ;
55+ await expect ( builtQueryString ) . toEqual ( `?foo_bar=${ mockParams . fooBar } ` ) ;
5156 } ) ;
5257
53- it ( "accept object params" , ( ) => {
58+ it ( "accept object params" , async ( ) => {
5459 mockParams = { something : "else" , foo : { bar : "foobar" } } ;
55- const queryString = buildQueryString ( mockParams ) ;
56- expect ( queryString ) . toEqual ( `?something=else&foo[bar]=${ mockParams . foo . bar } ` ) ;
60+ const queryString = await buildQueryString ( mockParams ) ;
61+ await expect ( queryString ) . toEqual ( `?something=else&foo[bar]=${ mockParams . foo . bar } ` ) ;
62+ } ) ;
63+
64+ it ( "properly converts patient info to token" , async ( ) => {
65+ mockParams = {
66+ fooBar : "foobar" ,
67+ patient : { id : "patientId" } ,
68+ fullscriptOptions : mockFullscriptOptions ,
69+ } ;
70+
71+ window . fetch = jest . fn ( ( ) =>
72+ Promise . resolve ( {
73+ json : ( ) => Promise . resolve ( { data_token : mockDataToken } ) ,
74+ } )
75+ ) as jest . Mock ;
76+
77+ const builtQueryString = await buildQueryString ( mockParams ) ;
78+
79+ await expect ( builtQueryString ) . toEqual (
80+ `?data_token=${ encodeURIComponent ( mockDataToken ) } &foo_bar=${ mockParams . fooBar } `
81+ ) ;
82+ } ) ;
83+
84+ it ( "passes null data token if tokenization fails" , async ( ) => {
85+ mockParams = {
86+ fooBar : "foobar" ,
87+ patient : { id : "patientId" } ,
88+ fullscriptOptions : mockFullscriptOptions ,
89+ } ;
90+
91+ window . fetch = jest . fn ( ( ) =>
92+ Promise . reject ( {
93+ json : ( ) => Promise . resolve ( { error : "some error" } ) ,
94+ } )
95+ ) as jest . Mock ;
96+
97+ const builtQueryString = await buildQueryString ( mockParams ) ;
98+
99+ await expect ( builtQueryString ) . toEqual ( `?data_token=null&foo_bar=${ mockParams . fooBar } ` ) ;
57100 } ) ;
58101 } ) ;
59102} ) ;
0 commit comments