@@ -13,7 +13,7 @@ const data = {
1313} ;
1414const subject = "subject-x0" ;
1515
16- const cloudevent = new CloudEvent ( {
16+ let cloudevent = new CloudEvent ( {
1717 specversion : Version . V03 ,
1818 id,
1919 source,
@@ -46,8 +46,13 @@ describe("CloudEvents Spec v0.3", () => {
4646
4747 describe ( "OPTIONAL Attributes" , ( ) => {
4848 it ( "Should have 'datacontentencoding'" , ( ) => {
49- cloudevent . datacontentencoding = Constants . ENCODING_BASE64 ;
49+ cloudevent = cloudevent . cloneWith ( {
50+ datacontentencoding : Constants . ENCODING_BASE64 ,
51+ data : "SSB3YXMgZnVubnkg8J+Ygg==" ,
52+ } ) ;
5053 expect ( cloudevent . datacontentencoding ) . to . equal ( Constants . ENCODING_BASE64 ) ;
54+
55+ cloudevent = cloudevent . cloneWith ( { datacontentencoding : undefined , data : data } ) ;
5156 } ) ;
5257
5358 it ( "Should have 'datacontenttype'" , ( ) => {
@@ -71,116 +76,117 @@ describe("CloudEvents Spec v0.3", () => {
7176 } ) ;
7277
7378 it ( "Should have the 'extension1'" , ( ) => {
74- cloudevent . extension1 = "value1" ;
79+ cloudevent = cloudevent . cloneWith ( { extension1 : "value1" } ) ;
7580 expect ( cloudevent . extension1 ) . to . equal ( "value1" ) ;
7681 } ) ;
7782 } ) ;
7883
7984 describe ( "The Constraints check" , ( ) => {
8085 describe ( "'id'" , ( ) => {
81- it ( "should throw an error when is absent " , ( ) => {
82- delete cloudevent . id ;
83- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
84- cloudevent . id = id ;
86+ it ( "should throw an error when trying to remove " , ( ) => {
87+ expect ( ( ) => {
88+ delete cloudevent . id ;
89+ } ) . to . throw ( TypeError ) ;
8590 } ) ;
8691
87- it ( "should throw an error when is empty" , ( ) => {
88- cloudevent . id = "" ;
89- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
90- cloudevent . id = id ;
92+ it ( "defaut ID create when an empty string" , ( ) => {
93+ cloudevent = cloudevent . cloneWith ( { id : "" } ) ;
94+ expect ( cloudevent . id . length ) . to . be . greaterThan ( 0 ) ;
9195 } ) ;
9296 } ) ;
9397
9498 describe ( "'source'" , ( ) => {
95- it ( "should throw an error when is absent " , ( ) => {
96- delete cloudevent . source ;
97- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
98- cloudevent . source = source ;
99+ it ( "should throw an error when trying to remove " , ( ) => {
100+ expect ( ( ) => {
101+ delete cloudevent . source ;
102+ } ) . to . throw ( TypeError ) ;
99103 } ) ;
100104 } ) ;
101105
102106 describe ( "'specversion'" , ( ) => {
103- it ( "should throw an error when is absent " , ( ) => {
104- delete cloudevent . specversion ;
105- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
106- cloudevent . specversion = Version . V03 ;
107+ it ( "should throw an error when trying to remove " , ( ) => {
108+ expect ( ( ) => {
109+ delete cloudevent . specversion ;
110+ } ) . to . throw ( TypeError ) ;
107111 } ) ;
108112 } ) ;
109113
110114 describe ( "'type'" , ( ) => {
111- it ( "should throw an error when is absent " , ( ) => {
112- delete cloudevent . type ;
113- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
114- cloudevent . type = type ;
115+ it ( "should throw an error when trying to remove " , ( ) => {
116+ expect ( ( ) => {
117+ delete cloudevent . type ;
118+ } ) . to . throw ( TypeError ) ;
115119 } ) ;
116120
117121 it ( "should throw an error when is an empty string" , ( ) => {
118- cloudevent . type = "" ;
119- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
120- cloudevent . type = type ;
122+ expect ( ( ) => {
123+ cloudevent . cloneWith ( { type : "" } ) ;
124+ } ) . to . throw ( ValidationError , "invalid payload" ) ;
121125 } ) ;
122126
123127 it ( "must be a non-empty string" , ( ) => {
124- cloudevent . type = type ;
128+ cloudevent . cloneWith ( { type : type } ) ;
125129 expect ( cloudevent . type ) . to . equal ( type ) ;
126130 } ) ;
127131 } ) ;
128132
129133 describe ( "'datacontentencoding'" , ( ) => {
130134 it ( "should throw an error when is a unsupported encoding" , ( ) => {
131- cloudevent . data = "Y2xvdWRldmVudHMK" ;
132- cloudevent . datacontentencoding = Mode . BINARY ;
133- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
134- delete cloudevent . datacontentencoding ;
135- cloudevent . data = data ;
135+ expect ( ( ) => {
136+ cloudevent . cloneWith ( { data : "Y2xvdWRldmVudHMK" , datacontentencoding : Mode . BINARY } ) ;
137+ } ) . to . throw ( ValidationError , "invalid payload" ) ;
138+
139+ cloudevent . cloneWith ( { data : data , datacontentencoding : undefined } ) ;
136140 } ) ;
137141
138142 it ( "should throw an error when 'data' does not carry base64" , ( ) => {
139- cloudevent . data = "no base 64 value" ;
140- cloudevent . datacontentencoding = Constants . ENCODING_BASE64 ;
141- cloudevent . datacontenttype = "text/plain" ;
142-
143- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
144-
145- delete cloudevent . datacontentencoding ;
146- cloudevent . data = data ;
143+ expect ( ( ) => {
144+ cloudevent . cloneWith ( {
145+ data : "no base 64 value" ,
146+ datacontentencoding : Constants . ENCODING_BASE64 ,
147+ datacontenttype : "text/plain" ,
148+ } ) ;
149+ } ) . to . throw ( ValidationError , "invalid payload" ) ;
150+
151+ cloudevent . cloneWith ( {
152+ data : data ,
153+ datacontentencoding : undefined ,
154+ } ) ;
147155 } ) ;
148156
149157 it ( "should accept when 'data' is a string" , ( ) => {
150- cloudevent . data = "Y2xvdWRldmVudHMK" ;
151- cloudevent . datacontentencoding = Constants . ENCODING_BASE64 ;
158+ cloudevent . cloneWith ( { data : "Y2xvdWRldmVudHMK" , datacontentencoding : Constants . ENCODING_BASE64 } ) ;
152159 expect ( cloudevent . validate ( ) ) . to . be . true ;
153- delete cloudevent . datacontentencoding ;
154- cloudevent . data = data ;
160+ cloudevent . cloneWith ( { data : data , datacontentencoding : undefined } ) ;
155161 } ) ;
156162 } ) ;
157163
158164 describe ( "'data'" , ( ) => {
159165 it ( "should maintain the type of data when no data content type" , ( ) => {
160- delete cloudevent . datacontenttype ;
166+ cloudevent = cloudevent . cloneWith ( { datacontenttype : undefined } ) ;
161167 cloudevent . data = JSON . stringify ( data ) ;
162168
163169 expect ( typeof cloudevent . data ) . to . equal ( "string" ) ;
164- cloudevent . datacontenttype = Constants . MIME_JSON ;
165170 } ) ;
166171
167172 it ( "should convert data with stringified json to a json object" , ( ) => {
168- cloudevent . datacontenttype = Constants . MIME_JSON ;
173+ cloudevent = cloudevent . cloneWith ( { datacontenttype : Constants . MIME_JSON } ) ;
169174 cloudevent . data = JSON . stringify ( data ) ;
170175 expect ( cloudevent . data ) . to . deep . equal ( data ) ;
171176 } ) ;
172177 } ) ;
173178
174179 describe ( "'subject'" , ( ) => {
175180 it ( "should throw an error when is an empty string" , ( ) => {
176- cloudevent . subject = "" ;
177- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
178- cloudevent . subject = subject ;
181+ expect ( ( ) => {
182+ cloudevent . cloneWith ( { subject : "" } ) ;
183+ } ) . to . throw ( ValidationError ) ;
179184 } ) ;
180185 } ) ;
181186
182187 describe ( "'time'" , ( ) => {
183188 it ( "must adhere to the format specified in RFC 3339" , ( ) => {
189+ cloudevent = cloudevent . cloneWith ( { time : time } ) ;
184190 expect ( cloudevent . time ) . to . equal ( time . toISOString ( ) ) ;
185191 } ) ;
186192 } ) ;
0 commit comments