11import {
22 config ,
33 toEmit ,
4- toHaveEmitted
4+ toHaveEmitted ,
5+ toHaveEmittedOnRoot
56} from "@/index" ;
67
78import Component from "./fixtures/event.vue" ;
@@ -10,7 +11,8 @@ import { MatcherResult } from '@/utils';
1011
1112expect . extend ( {
1213 toEmit,
13- toHaveEmitted
14+ toHaveEmitted,
15+ toHaveEmittedOnRoot
1416} ) ;
1517
1618config ( {
@@ -21,6 +23,10 @@ const emitEvent = (wrapper, eventName, payload) => {
2123 ( wrapper . vm as any ) . emitEventWithPayload ( eventName , payload ) ;
2224} ;
2325
26+ const emitEventOnRoot = ( wrapper , eventName , payload ) => {
27+ ( wrapper . vm as any ) . emitEventOnRootWithPayload ( eventName , payload ) ;
28+ } ;
29+
2430const doAsyncronously = ( callback : Function ) : Promise < any > => {
2531 return new Promise ( ( resolve ) => {
2632 callback ( ) ;
@@ -178,6 +184,156 @@ describe("toHaveEmitted with payload", () => {
178184 } ) ;
179185} ) ;
180186
187+ describe ( "toHaveEmittedOnRoot" , ( ) => {
188+ describe ( "as a function which is registered to jest" , ( ) => {
189+ it ( "returns true if the event is emitted on $root" , ( ) => {
190+ const wrapper = shallowMount ( Component ) ;
191+ wrapper . trigger ( "blur" ) ;
192+ const result = toHaveEmittedOnRoot ( wrapper , "blured" ) ;
193+ expect ( result . pass ) . toBe ( true ) ;
194+ expect ( result . message ( ) ) . toBe ( 'The "blured" event was emitted on $root' ) ;
195+ } ) ;
196+
197+ it ( "returns false if the event is not emitted on $root" , ( ) => {
198+ const wrapper = shallowMount ( Component ) ;
199+ const result = toHaveEmittedOnRoot ( wrapper , "blured" ) ;
200+ expect ( result . pass ) . toBe ( false ) ;
201+ expect ( result . message ( ) ) . toBe ( 'The "blured" event was never emitted on $root' ) ;
202+ } ) ;
203+
204+ it ( "returns false if the another event is emitted on $root" , ( ) => {
205+ const wrapper = shallowMount ( Component ) ;
206+ wrapper . vm . $root . $emit ( "another" ) ;
207+ const result = toHaveEmittedOnRoot ( wrapper , "blured" ) ;
208+ expect ( result . pass ) . toBe ( false ) ;
209+ expect ( result . message ( ) ) . toBe ( 'The "blured" event was never emitted on $root' ) ;
210+ } ) ;
211+ } ) ;
212+
213+ describe ( "actual use" , ( ) => {
214+ it ( "doesn't claim for positive expectation when expected event happens" , ( ) => {
215+ const wrapper = shallowMount ( Component ) ;
216+ wrapper . trigger ( "blur" ) ;
217+ expect ( wrapper ) . toHaveEmittedOnRoot ( "blured" ) ;
218+ } ) ;
219+
220+ it ( "doesn't claim for negative expectation when expected event doesn't happen" , ( ) => {
221+ const wrapper = shallowMount ( Component ) ;
222+ expect ( wrapper ) . not . toHaveEmittedOnRoot ( "blured" ) ;
223+ } ) ;
224+
225+ it ( "doesn't claim for negative expectation when unintentional event happens" , ( ) => {
226+ const wrapper = shallowMount ( Component ) ;
227+ wrapper . vm . $root . $emit ( "another" ) ;
228+ expect ( wrapper ) . not . toHaveEmittedOnRoot ( "blured" ) ;
229+ } ) ;
230+ } ) ;
231+ } ) ;
232+
233+ describe ( "toHaveEmittedOnRoot with payload" , ( ) => {
234+ describe ( "as a function which is registered to jest" , ( ) => {
235+ it ( "returns true if the event is emitted with the expected payload" , ( ) => {
236+ const wrapper = shallowMount ( Component ) ;
237+ emitEventOnRoot ( wrapper , "rooty" , { value : "something" } ) ;
238+ const result = toHaveEmittedOnRoot ( wrapper , "rooty" , { value : "something" } ) ;
239+ expect ( result . pass ) . toBe ( true ) ;
240+ expect ( result . message ( ) ) . toBe ( 'The "rooty" event was emitted on $root with the expected payload' ) ;
241+ } ) ;
242+
243+ it ( "returns false if the event is not emitted" , ( ) => {
244+ const wrapper = shallowMount ( Component ) ;
245+ const result = toHaveEmittedOnRoot ( wrapper , "rooty" , "something" ) ;
246+ expect ( result . pass ) . toBe ( false ) ;
247+ expect ( result . message ( ) ) . toBe ( 'The "rooty" event was never emitted on $root' ) ;
248+ } ) ;
249+
250+ it ( "returns false if the another event is emitted" , ( ) => {
251+ const wrapper = shallowMount ( Component ) ;
252+ emitEventOnRoot ( wrapper , "another" , { value : "something" } ) ;
253+ const result = toHaveEmittedOnRoot ( wrapper , "rooty" , { value : "something" } ) ;
254+ expect ( result . pass ) . toBe ( false ) ;
255+ expect ( result . message ( ) ) . toBe ( 'The "rooty" event was never emitted on $root' ) ;
256+ } ) ;
257+
258+ describe ( "when the event is emitted but the payload is not matched" , ( ) => {
259+ const subject = ( ) => {
260+ const wrapper = shallowMount ( Component ) ;
261+ emitEventOnRoot ( wrapper , "rooty" , { value : "anything" } ) ;
262+ return toHaveEmittedOnRoot ( wrapper , "rooty" , "some text" , { value : "something" } ) ;
263+ } ;
264+
265+ it ( "returns false" , ( ) => {
266+ expect ( subject ( ) . pass ) . toBe ( false ) ;
267+ } ) ;
268+
269+ it ( "tells the reason" , ( ) => {
270+ expect ( subject ( ) . message ( ) ) . toContain ( 'The "rooty" event was emitted on $root but the payload is not matched' ) ;
271+ } ) ;
272+
273+ it ( "shows the diff of payloads" , ( ) => {
274+ const message = subject ( ) . message ( ) ;
275+ expect ( message ) . toContain ( "'rooty' event #0 payloads:" ) ;
276+ expect ( message ) . toContain ( "- Expected" ) ;
277+ expect ( message ) . toContain ( "+ Emitted" ) ;
278+ expect ( message ) . toContain ( '- "some text"' ) ;
279+ expect ( message ) . toContain ( '- "value": "something"' ) ;
280+ expect ( message ) . toContain ( '+ "value": "anything"' ) ;
281+ } ) ;
282+ } ) ;
283+ } ) ;
284+
285+ describe ( "actual use" , ( ) => {
286+ it ( "passes for expected case positively" , ( ) => {
287+ const wrapper = shallowMount ( Component ) ;
288+ emitEventOnRoot ( wrapper , "rooty" , { value : "actual life" } ) ;
289+ expect ( wrapper ) . toHaveEmittedOnRoot ( "rooty" , { value : "actual life" } ) ;
290+ } ) ;
291+
292+ it ( "passes negatively when any event was not emitted" , ( ) => {
293+ const wrapper = shallowMount ( Component ) ;
294+ expect ( wrapper ) . not . toHaveEmittedOnRoot ( "rooty" , { value : "unusual life" } ) ;
295+ } ) ;
296+
297+ it ( "passes negatively when unintentional event was emitted" , ( ) => {
298+ const wrapper = shallowMount ( Component ) ;
299+ emitEventOnRoot ( wrapper , "another" , { value : "actual life" } ) ;
300+ expect ( wrapper ) . not . toHaveEmittedOnRoot ( "rooty" , { value : "unusual life" } ) ;
301+ } ) ;
302+
303+ it ( "passes negatively when the expected event is emitted but the payload is not matched" , ( ) => {
304+ const wrapper = shallowMount ( Component ) ;
305+ emitEventOnRoot ( wrapper , "rooty" , { value : "actual life" } ) ;
306+ expect ( wrapper ) . not . toHaveEmitted ( "rooty" , { value : "exciting life" } ) ;
307+ } ) ;
308+
309+ describe ( "multiple payloads" , ( ) => {
310+ it ( "passes positively when the expected event is emitted with the multiple payloads by the function" , ( ) => {
311+ const wrapper = shallowMount ( Component ) ;
312+ ( wrapper . vm as any ) . emitEventOnRootWithMultiplePayload ( "multiOnRoot!" ) ;
313+ expect ( wrapper ) . toHaveEmittedOnRoot ( "multiOnRoot!" , [ "D" ] , 3 , "e" ) ;
314+ } ) ;
315+
316+ it ( "passes positively for expect helper to assert with 'anything'" , ( ) => {
317+ const wrapper = shallowMount ( Component ) ;
318+ ( wrapper . vm as any ) . emitEventOnRootWithMultiplePayload ( "multiOnRoot!!" ) ;
319+ expect ( wrapper ) . toHaveEmittedOnRoot ( "multiOnRoot!!" , expect . arrayContaining ( [ "D" ] ) , expect . anything ( ) , "e" ) ;
320+ } ) ;
321+
322+ it ( "passes negatively when the number of paylod is shorter than actual (even they are matching)" , ( ) => {
323+ const wrapper = shallowMount ( Component ) ;
324+ ( wrapper . vm as any ) . emitEventOnRootWithMultiplePayload ( "multiOnRoot!" ) ;
325+ expect ( wrapper ) . not . toHaveEmittedOnRoot ( "multiOnRoot!" , [ "D" ] , 3 ) ;
326+ } ) ;
327+
328+ it ( "passes negatively when the number of paylod is longer" , ( ) => {
329+ const wrapper = shallowMount ( Component ) ;
330+ ( wrapper . vm as any ) . emitEventOnRootWithMultiplePayload ( "multiOnRoot!" ) ;
331+ expect ( wrapper ) . not . toHaveEmittedOnRoot ( "multiOnRoot!" , [ "D" ] , 3 , "e" , "additional" ) ;
332+ } ) ;
333+ } ) ;
334+ } ) ;
335+ } ) ;
336+
181337describe ( "toEmit" , ( ) => {
182338 describe ( "as a function which is registered to jest" , ( ) => {
183339 describe ( "returns true if the event is emitted" , ( ) => {
0 commit comments