@@ -14,7 +14,7 @@ const data = {
1414} ;
1515const subject = "subject-x0" ;
1616
17- const cloudevent = new CloudEvent ( {
17+ let cloudevent = new CloudEvent ( {
1818 specversion : Version . V1 ,
1919 id,
2020 source,
@@ -65,30 +65,30 @@ describe("CloudEvents Spec v1.0", () => {
6565
6666 describe ( "Extensions Constraints" , ( ) => {
6767 it ( "should be ok when type is 'boolean'" , ( ) => {
68- cloudevent [ "ext-boolean" ] = true ;
68+ cloudevent = cloudevent . cloneWith ( { "ext-boolean" : true } ) ;
6969 expect ( cloudevent . validate ( ) ) . to . equal ( true ) ;
7070 } ) ;
7171
7272 it ( "should be ok when type is 'integer'" , ( ) => {
73- cloudevent [ "ext-integer" ] = 2019 ;
73+ cloudevent = cloudevent . cloneWith ( { "ext-integer" : 2019 } ) ;
7474 expect ( cloudevent . validate ( ) ) . to . equal ( true ) ;
7575 } ) ;
7676
7777 it ( "should be ok when type is 'string'" , ( ) => {
78- cloudevent [ "ext-string" ] = "an-string" ;
78+ cloudevent = cloudevent . cloneWith ( { "ext-string" : "an-string" } ) ;
7979 expect ( cloudevent . validate ( ) ) . to . equal ( true ) ;
8080 } ) ;
8181
8282 it ( "should be ok when type is 'Uint32Array' for 'Binary'" , ( ) => {
8383 const myBinary = new Uint32Array ( 2019 ) ;
84- cloudevent [ "ext-binary" ] = myBinary ;
84+ cloudevent = cloudevent . cloneWith ( { "ext-binary" : myBinary } ) ;
8585 expect ( cloudevent . validate ( ) ) . to . equal ( true ) ;
8686 } ) ;
8787
8888 // URI
8989 it ( "should be ok when type is 'Date' for 'Timestamp'" , ( ) => {
9090 const myDate = new Date ( ) ;
91- cloudevent [ "ext-date" ] = myDate ;
91+ cloudevent = cloudevent . cloneWith ( { "ext-date" : myDate } ) ;
9292 expect ( cloudevent . validate ( ) ) . to . equal ( true ) ;
9393 } ) ;
9494
@@ -97,73 +97,60 @@ describe("CloudEvents Spec v1.0", () => {
9797 // is transmitted across the wire, this value will be
9898 // converted to JSON
9999 it ( "should be ok when the type is an object" , ( ) => {
100- cloudevent [ "object-extension" ] = { some : "object" } ;
100+ cloudevent = cloudevent . cloneWith ( { "object-extension" : { some : "object" } } ) ;
101101 expect ( cloudevent . validate ( ) ) . to . equal ( true ) ;
102102 } ) ;
103103 } ) ;
104104
105105 describe ( "The Constraints check" , ( ) => {
106106 describe ( "'id'" , ( ) => {
107- it ( "should throw an error when is absent " , ( ) => {
108- delete cloudevent . id ;
109- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
110- cloudevent . id = id ;
107+ it ( "should throw an error when trying to remove " , ( ) => {
108+ expect ( ( ) => {
109+ delete cloudevent . id ;
110+ } ) . to . throw ( ) ;
111111 } ) ;
112112
113- it ( "should throw an error when is empty" , ( ) => {
114- cloudevent . id = "" ;
115- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
116- cloudevent . id = id ;
113+ it ( "defaut ID create when an empty string" , ( ) => {
114+ cloudevent = cloudevent . cloneWith ( { id : "" } ) ;
115+ expect ( cloudevent . id . length ) . to . be . greaterThan ( 0 ) ;
117116 } ) ;
118117 } ) ;
119118
120119 describe ( "'source'" , ( ) => {
121- it ( "should throw an error when is absent " , ( ) => {
122- delete cloudevent . source ;
123- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
124- cloudevent . source = source ;
120+ it ( "should throw an error when trying to remove " , ( ) => {
121+ expect ( ( ) => {
122+ delete cloudevent . source ;
123+ } ) . to . throw ( ) ;
125124 } ) ;
126125 } ) ;
127126
128127 describe ( "'specversion'" , ( ) => {
129- it ( "should throw an error when is absent" , ( ) => {
130- delete cloudevent . specversion ;
131- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
132- cloudevent . specversion = Version . V1 ;
133- } ) ;
134-
135- it ( "should throw an error when is empty" , ( ) => {
136- cloudevent . specversion = "" as Version ;
137- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
138- cloudevent . specversion = Version . V1 ;
128+ it ( "should throw an error when trying to remove" , ( ) => {
129+ expect ( ( ) => {
130+ delete cloudevent . specversion ;
131+ } ) . to . throw ( ) ;
139132 } ) ;
140133 } ) ;
141134
142135 describe ( "'type'" , ( ) => {
143- it ( "should throw an error when is absent" , ( ) => {
144- delete cloudevent . type ;
145- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
146- cloudevent . type = type ;
147- } ) ;
148-
149- it ( "should throw an error when is an empty string" , ( ) => {
150- cloudevent . type = "" ;
151- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
152- cloudevent . type = type ;
136+ it ( "should throw an error when trying to remove" , ( ) => {
137+ expect ( ( ) => {
138+ delete cloudevent . type ;
139+ } ) . to . throw ( ) ;
153140 } ) ;
154141 } ) ;
155142
156143 describe ( "'subject'" , ( ) => {
157144 it ( "should throw an error when is an empty string" , ( ) => {
158- cloudevent . subject = "" ;
159- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
160- cloudevent . subject = type ;
145+ expect ( ( ) => {
146+ cloudevent . cloneWith ( { subject : "" } ) ;
147+ } ) . to . throw ( ValidationError ) ;
161148 } ) ;
162149 } ) ;
163150
164151 describe ( "'time'" , ( ) => {
165152 it ( "must adhere to the format specified in RFC 3339" , ( ) => {
166- cloudevent . time = time ;
153+ cloudevent = cloudevent . cloneWith ( { time : time } ) ;
167154 expect ( cloudevent . time ) . to . equal ( time . toISOString ( ) ) ;
168155 } ) ;
169156 } ) ;
@@ -176,16 +163,15 @@ describe("CloudEvents Spec v1.0", () => {
176163
177164 it ( "should maintain the type of data when no data content type" , ( ) => {
178165 const dct = cloudevent . datacontenttype ;
179- delete cloudevent . datacontenttype ;
166+ cloudevent = cloudevent . cloneWith ( { datacontenttype : undefined } ) ;
180167 cloudevent . data = JSON . stringify ( data ) ;
181168
182169 expect ( typeof cloudevent . data ) . to . equal ( "string" ) ;
183- cloudevent . datacontenttype = dct ;
170+ cloudevent = cloudevent . cloneWith ( { datacontenttype : dct } ) ;
184171 } ) ;
185172
186173 it ( "should convert data with stringified json to a json object" , ( ) => {
187- cloudevent . datacontenttype = Constants . MIME_JSON ;
188- cloudevent . data = JSON . stringify ( data ) ;
174+ cloudevent = cloudevent . cloneWith ( { datacontenttype : Constants . MIME_JSON , data : JSON . stringify ( data ) } ) ;
189175 expect ( cloudevent . data ) . to . deep . equal ( data ) ;
190176 } ) ;
191177
@@ -194,13 +180,10 @@ describe("CloudEvents Spec v1.0", () => {
194180
195181 const dataBinary = Uint32Array . from ( dataString , ( c ) => c . codePointAt ( 0 ) as number ) ;
196182 const expected = asBase64 ( dataBinary ) ;
197- const olddct = cloudevent . datacontenttype ;
198183
199- cloudevent . datacontenttype = "text/plain" ;
200- cloudevent . data = dataBinary ;
201- expect ( cloudevent . data_base64 ) . to . equal ( expected ) ;
184+ cloudevent = cloudevent . cloneWith ( { datacontenttype : "text/plain" , data : dataBinary } ) ;
202185
203- cloudevent . datacontenttype = olddct ;
186+ expect ( cloudevent . data_base64 ) . to . equal ( expected ) ;
204187 } ) ;
205188 } ) ;
206189} ) ;
0 commit comments