@@ -13,65 +13,153 @@ var dateReviver = require('../lib/requests/helpers/dateReviver');
1313var Request = require ( '../lib/requests/sendRequest' ) ;
1414var parseResponse = require ( '../lib/requests/helpers/parseResponse' ) ;
1515
16- describe ( "Utility.buildFunctionParameters - " , function ( ) {
17- it ( "no parameters" , function ( ) {
18- var result = Utility . buildFunctionParameters ( ) ;
19- expect ( result ) . to . equal ( "()" ) ;
20- } ) ;
21- it ( "1 parameter" , function ( ) {
22- var result = Utility . buildFunctionParameters ( { param1 : "value1" } ) ;
23- expect ( result ) . to . equal ( "(param1=@p1)?@p1='value1'" ) ;
24- } ) ;
25- it ( "2 parameters" , function ( ) {
26- var result = Utility . buildFunctionParameters ( { param1 : "value1" , param2 : 2 } ) ;
27- expect ( result ) . to . equal ( "(param1=@p1,param2=@p2)?@p1='value1'&@p2=2" ) ;
16+ describe ( "Utility." , function ( ) {
17+ describe ( "buildFunctionParameters - " , function ( ) {
18+ it ( "no parameters" , function ( ) {
19+ var result = Utility . buildFunctionParameters ( ) ;
20+ expect ( result ) . to . equal ( "()" ) ;
21+ } ) ;
22+ it ( "1 parameter" , function ( ) {
23+ var result = Utility . buildFunctionParameters ( { param1 : "value1" } ) ;
24+ expect ( result ) . to . equal ( "(param1=@p1)?@p1='value1'" ) ;
25+ } ) ;
26+ it ( "2 parameters" , function ( ) {
27+ var result = Utility . buildFunctionParameters ( { param1 : "value1" , param2 : 2 } ) ;
28+ expect ( result ) . to . equal ( "(param1=@p1,param2=@p2)?@p1='value1'&@p2=2" ) ;
29+ } ) ;
30+ it ( "3 parameters" , function ( ) {
31+ var result = Utility . buildFunctionParameters ( { param1 : "value1" , param2 : 2 , param3 : "value2" } ) ;
32+ expect ( result ) . to . equal ( "(param1=@p1,param2=@p2,param3=@p3)?@p1='value1'&@p2=2&@p3='value2'" ) ;
33+ } ) ;
34+ it ( "object parameter" , function ( ) {
35+ var result = Utility . buildFunctionParameters ( { param1 : { test1 : "value" , '@odata.type' : 'account' } } ) ;
36+ expect ( result ) . to . equal ( "(param1=@p1)?@p1={\"test1\":\"value\",\"@odata.type\":\"account\"}" ) ;
37+ } ) ;
2838 } ) ;
29- it ( "3 parameters" , function ( ) {
30- var result = Utility . buildFunctionParameters ( { param1 : "value1" , param2 : 2 , param3 : "value2" } ) ;
31- expect ( result ) . to . equal ( "(param1=@p1,param2=@p2,param3=@p3)?@p1='value1'&@p2=2&@p3='value2'" ) ;
39+
40+ describe ( "getFetchXmlPagingCookie - " , function ( ) {
41+ it ( "pagingCookie is empty" , function ( ) {
42+ var result = Utility . getFetchXmlPagingCookie ( "" , 2 ) ;
43+ expect ( result ) . to . deep . equal ( {
44+ cookie : "" ,
45+ page : 2 ,
46+ nextPage : 3
47+ } ) ;
48+ } ) ;
49+
50+ it ( "pagingCookie is null or undefined" , function ( ) {
51+ var result = Utility . getFetchXmlPagingCookie ( null , 2 ) ;
52+ expect ( result ) . to . deep . equal ( {
53+ cookie : "" ,
54+ page : 2 ,
55+ nextPage : 3
56+ } ) ;
57+
58+ result = Utility . getFetchXmlPagingCookie ( ) ;
59+ expect ( result ) . to . deep . equal ( {
60+ cookie : "" ,
61+ page : 1 ,
62+ nextPage : 2
63+ } ) ;
64+ } ) ;
65+
66+ it ( "pagingCookie is normal" , function ( ) {
67+ var result = Utility . getFetchXmlPagingCookie ( mocks . data . fetchXmls . cookiePage2 , 2 ) ;
68+ expect ( result ) . to . deep . equal ( mocks . data . fetchXmls . fetchXmlResultPage2Cookie . PagingInfo ) ;
69+
70+ result = Utility . getFetchXmlPagingCookie ( mocks . data . fetchXmls . cookiePage1 , 2 ) ;
71+ expect ( result ) . to . deep . equal ( mocks . data . fetchXmls . fetchXmlResultPage1Cookie . PagingInfo ) ;
72+
73+ result = Utility . getFetchXmlPagingCookie ( mocks . data . fetchXmls . cookiePage2 ) ;
74+ expect ( result ) . to . deep . equal ( mocks . data . fetchXmls . fetchXmlResultPage2Cookie . PagingInfo ) ;
75+
76+ } ) ;
3277 } ) ;
33- it ( "object parameter" , function ( ) {
34- var result = Utility . buildFunctionParameters ( { param1 : { test1 : "value" , '@odata.type' : 'account' } } ) ;
35- expect ( result ) . to . equal ( "(param1=@p1)?@p1={\"test1\":\"value\",\"@odata.type\":\"account\"}" ) ;
78+
79+ describe ( "getXrmContext - GetGlobalContext" , function ( ) {
80+ before ( function ( ) {
81+ global . GetGlobalContext = function ( ) {
82+ return "Global Context" ;
83+ } ;
84+ } ) ;
85+
86+ after ( function ( ) {
87+ global . GetGlobalContext = undefined ;
88+ } ) ;
89+
90+ it ( "returns a correct object" , function ( ) {
91+ var result = Utility . getXrmContext ( ) ;
92+
93+ expect ( result ) . to . be . eq ( "Global Context" ) ;
94+ } ) ;
3695 } ) ;
37- } ) ;
3896
39- describe ( "Utility.getFetchXmlPagingCookie -" , function ( ) {
40- it ( "pagingCookie is empty" , function ( ) {
41- var result = Utility . getFetchXmlPagingCookie ( "" , 2 ) ;
42- expect ( result ) . to . deep . equal ( {
43- cookie : "" ,
44- page : 2 ,
45- nextPage : 3
97+ describe ( "getXrmContext - Xrm.Utility.getGlobalContext" , function ( ) {
98+ before ( function ( ) {
99+ global . Xrm . Utility = {
100+ getGlobalContext : function ( ) {
101+ return {
102+ getClientUrl : function ( ) {
103+ return "Xrm.Utility" ;
104+ }
105+ } ;
106+ }
107+ } ;
108+ } ) ;
109+
110+ after ( function ( ) {
111+ global . Xrm . Utility = undefined ;
112+ } ) ;
113+
114+ it ( "returns a correct object" , function ( ) {
115+ var result = Utility . getXrmContext ( ) . getClientUrl ( ) ;
116+
117+ expect ( result ) . to . be . eq ( "Xrm.Utility" ) ;
46118 } ) ;
47119 } ) ;
48120
49- it ( "pagingCookie is null or undefined" , function ( ) {
50- var result = Utility . getFetchXmlPagingCookie ( null , 2 ) ;
51- expect ( result ) . to . deep . equal ( {
52- cookie : "" ,
53- page : 2 ,
54- nextPage : 3
121+ describe ( "getXrmContext - Form context does not exist" , function ( ) {
122+ before ( function ( ) {
123+ global . Xrm = undefined ;
55124 } ) ;
56125
57- result = Utility . getFetchXmlPagingCookie ( ) ;
58- expect ( result ) . to . deep . equal ( {
59- cookie : "" ,
60- page : 1 ,
61- nextPage : 2
126+ after ( function ( ) {
127+ global . Xrm = {
128+ Page : {
129+ context : {
130+ getClientUrl : function ( ) {
131+ return "http://testorg.crm.dynamics.com" ;
132+ }
133+ }
134+ }
135+ } ;
136+ } ) ;
137+
138+ it ( "throws an error" , function ( ) {
139+ expect ( function ( ) {
140+ Utility . getXrmContext ( ) ;
141+ } ) . to . throw ( ) ;
62142 } ) ;
63143 } ) ;
64144
65- it ( "pagingCookie is normal" , function ( ) {
66- var result = Utility . getFetchXmlPagingCookie ( mocks . data . fetchXmls . cookiePage2 , 2 ) ;
67- expect ( result ) . to . deep . equal ( mocks . data . fetchXmls . fetchXmlResultPage2Cookie . PagingInfo ) ;
145+ describe ( "getClientUrl - removes a slash at the end" , function ( ) {
146+ before ( function ( ) {
147+ Xrm . Page . context . getClientUrl = function ( ) {
148+ return "http://testorg.crm.dynamics.com/" ;
149+ } ;
150+ } ) ;
68151
69- result = Utility . getFetchXmlPagingCookie ( mocks . data . fetchXmls . cookiePage1 , 2 ) ;
70- expect ( result ) . to . deep . equal ( mocks . data . fetchXmls . fetchXmlResultPage1Cookie . PagingInfo ) ;
152+ after ( function ( ) {
153+ Xrm . Page . context . getClientUrl = function ( ) {
154+ return "http://testorg.crm.dynamics.com" ;
155+ } ;
156+ } ) ;
71157
72- result = Utility . getFetchXmlPagingCookie ( mocks . data . fetchXmls . cookiePage2 ) ;
73- expect ( result ) . to . deep . equal ( mocks . data . fetchXmls . fetchXmlResultPage2Cookie . PagingInfo ) ;
158+ it ( "returns a correct string" , function ( ) {
159+ var result = Utility . getClientUrl ( ) ;
74160
161+ expect ( result ) . to . be . eq ( "http://testorg.crm.dynamics.com" ) ;
162+ } ) ;
75163 } ) ;
76164} ) ;
77165
@@ -1772,6 +1860,96 @@ describe("Request.sendRequest", function () {
17721860 expect ( scope . isDone ( ) ) . to . be . true ;
17731861 } ) ;
17741862 } ) ;
1863+
1864+ describe ( "request error" , function ( ) {
1865+ var scope ;
1866+ var url = 'test' ;
1867+ before ( function ( ) {
1868+ scope = nock ( mocks . webApiUrl + 'test' )
1869+ . post ( "" , mocks . data . testEntity )
1870+ . replyWithError ( { code : 'Error' } ) ;
1871+ } ) ;
1872+
1873+ after ( function ( ) {
1874+ nock . cleanAll ( ) ;
1875+ } ) ;
1876+
1877+ it ( "returns a correct response" , function ( done ) {
1878+ Request . sendRequest ( 'POST' , url , { webApiUrl : mocks . webApiUrl } , mocks . data . testEntityAdditionalAttributes , null , null , function ( object ) {
1879+ expect ( object ) . to . be . undefined ;
1880+ done ( object ) ;
1881+ } , function ( object ) {
1882+ expect ( object ) . to . be . deep . equal ( { code : "Error" } ) ;
1883+ done ( ) ;
1884+ } ) ;
1885+ } ) ;
1886+
1887+ it ( "all requests have been made" , function ( ) {
1888+ expect ( scope . isDone ( ) ) . to . be . true ;
1889+ } ) ;
1890+ } ) ;
1891+
1892+ describe ( "timeout - socket" , function ( ) {
1893+ var scope ;
1894+ var url = 'test' ;
1895+ before ( function ( ) {
1896+ var response = mocks . responses . basicEmptyResponseSuccess ;
1897+ scope = nock ( mocks . webApiUrl + 'test' )
1898+ . post ( "" , mocks . data . testEntity )
1899+ . socketDelay ( 1000 )
1900+ . reply ( response . status , response . responseText , response . responseHeaders ) ;
1901+ } ) ;
1902+
1903+ after ( function ( ) {
1904+ nock . cleanAll ( ) ;
1905+ } ) ;
1906+
1907+ it ( "returns a correct response" , function ( done ) {
1908+ Request . sendRequest ( 'POST' , url , { webApiUrl : mocks . webApiUrl , timeout : 500 } , mocks . data . testEntityAdditionalAttributes , null , null , function ( object ) {
1909+ expect ( object ) . to . be . undefined ;
1910+ done ( object ) ;
1911+ } , function ( error ) {
1912+ expect ( error . message ) . to . be . eq ( "socket hang up" ) ;
1913+ expect ( error . code ) . to . be . eq ( "ECONNRESET" ) ;
1914+ done ( ) ;
1915+ } ) ;
1916+ } ) ;
1917+
1918+ it ( "all requests have been made" , function ( ) {
1919+ expect ( scope . isDone ( ) ) . to . be . true ;
1920+ } ) ;
1921+ } ) ;
1922+
1923+ describe ( "timeout - connection delay" , function ( ) {
1924+ var scope ;
1925+ var url = 'test' ;
1926+ before ( function ( ) {
1927+ var response = mocks . responses . basicEmptyResponseSuccess ;
1928+ scope = nock ( mocks . webApiUrl + 'test' )
1929+ . post ( "" , mocks . data . testEntity )
1930+ . delayConnection ( 1000 )
1931+ . reply ( response . status , response . responseText , response . responseHeaders ) ;
1932+ } ) ;
1933+
1934+ after ( function ( ) {
1935+ nock . cleanAll ( ) ;
1936+ } ) ;
1937+
1938+ it ( "returns a correct response" , function ( done ) {
1939+ Request . sendRequest ( 'POST' , url , { webApiUrl : mocks . webApiUrl , timeout : 500 } , mocks . data . testEntityAdditionalAttributes , null , null , function ( object ) {
1940+ expect ( object ) . to . be . undefined ;
1941+ done ( object ) ;
1942+ } , function ( error ) {
1943+ expect ( error . message ) . to . be . eq ( "socket hang up" ) ;
1944+ expect ( error . code ) . to . be . eq ( "ECONNRESET" ) ;
1945+ done ( ) ;
1946+ } ) ;
1947+ } ) ;
1948+
1949+ it ( "all requests have been made" , function ( ) {
1950+ expect ( scope . isDone ( ) ) . to . be . true ;
1951+ } ) ;
1952+ } ) ;
17751953} ) ;
17761954
17771955describe ( "parseResponse" , function ( ) {
0 commit comments