@@ -21,6 +21,56 @@ describe('AvMessage', () => {
21
21
expect ( avMessage . enabled ( 'hello' ) ) . toBe ( true ) ;
22
22
} ) ;
23
23
24
+ describe ( 'subscribers' , ( ) => {
25
+ test ( 'onMessage should call all subscribers for event' , ( ) => {
26
+ const testEvent = 'testEvent' ;
27
+ const fns = [ jest . fn ( ) , jest . fn ( ) ] ;
28
+ avMessage . subscribers = {
29
+ [ testEvent ] : fns ,
30
+ } ;
31
+ avMessage . onMessage ( `${ testEvent } Other` ) ;
32
+ fns . forEach ( fn => expect ( fn ) . not . toHaveBeenCalled ( ) ) ;
33
+
34
+ const data = { testData : 'hello world' } ;
35
+ avMessage . onMessage ( testEvent , data ) ;
36
+ fns . forEach ( fn => expect ( fn ) . toHaveBeenCalledWith ( data ) ) ;
37
+ } ) ;
38
+
39
+ test ( 'subscribe should add function to subscribers' , ( ) => {
40
+ avMessage . subscribers = { } ;
41
+ const testEvent = 'testEvent' ;
42
+ const fn = 'totally a function' ;
43
+ avMessage . subscribe ( testEvent , fn ) ;
44
+ expect ( avMessage . subscribers ) . toEqual ( {
45
+ [ testEvent ] : [ fn ] ,
46
+ } ) ;
47
+
48
+ const fn2 = 'totally another function' ;
49
+ avMessage . subscribe ( testEvent , fn2 ) ;
50
+ expect ( avMessage . subscribers ) . toEqual ( {
51
+ [ testEvent ] : [ fn , fn2 ] ,
52
+ } ) ;
53
+ } ) ;
54
+
55
+ test ( 'subscribe should return function to remove subscribers' , ( ) => {
56
+ avMessage . subscribers = { } ;
57
+ const testEvent = 'testEvent' ;
58
+ const fn = 'totally a function' ;
59
+ const unsubscribe = avMessage . subscribe ( testEvent , fn ) ;
60
+
61
+ const fn2 = 'totally another function' ;
62
+ avMessage . subscribe ( testEvent , fn2 ) ;
63
+ expect ( avMessage . subscribers ) . toEqual ( {
64
+ [ testEvent ] : [ fn , fn2 ] ,
65
+ } ) ;
66
+
67
+ unsubscribe ( ) ;
68
+ expect ( avMessage . subscribers ) . toEqual ( {
69
+ [ testEvent ] : [ fn2 ] ,
70
+ } ) ;
71
+ } ) ;
72
+ } ) ;
73
+
24
74
describe ( 'getEventData()' , ( ) => {
25
75
let spyParse ;
26
76
const mockEvent = {
@@ -49,20 +99,6 @@ describe('AvMessage', () => {
49
99
expect ( avMessage . onMessage ) . not . toHaveBeenCalled ( ) ;
50
100
} ) ;
51
101
52
- test ( 'should return early when AvMessages.onMessage not defined' , ( ) => {
53
- delete avMessage . onMessage ;
54
- avMessage . getEventData ( mockEvent ) ;
55
- expect ( avMessage . isDomain ) . not . toHaveBeenCalled ( ) ;
56
- expect ( spyParse ) . not . toHaveBeenCalled ( ) ;
57
- } ) ;
58
-
59
- test ( 'should return early when AvMessages.onMessage not function' , ( ) => {
60
- avMessage . onMessage = 'onMessage' ;
61
- avMessage . getEventData ( mockEvent ) ;
62
- expect ( avMessage . isDomain ) . not . toHaveBeenCalled ( ) ;
63
- expect ( spyParse ) . not . toHaveBeenCalled ( ) ;
64
- } ) ;
65
-
66
102
test ( 'should return early when event does not have all fields' , ( ) => {
67
103
const mockEvent1 = Object . assign ( { } , mockEvent , { data : false } ) ;
68
104
const mockEvent2 = Object . assign ( { } , mockEvent , { origin : false } ) ;
@@ -153,14 +189,6 @@ describe('AvMessage', () => {
153
189
test ( 'should return location.origin if exists' , ( ) => {
154
190
expect ( avMessage . domain ( ) ) . toBe ( URL ) ;
155
191
} ) ;
156
-
157
- // test('if no location.origin, should return domain generated with hostname', () => {
158
- // expect(avMessage.domain()).toBe(URL);
159
- // });
160
-
161
- // test("if no location origin or hostname, should return '*'", () => {
162
- // expect(avMessage.domain()).toBe(URL);
163
- // });
164
192
} ) ;
165
193
166
194
test ( "isDomain should return true if domain() doesn't match regex" , ( ) => {
0 commit comments