1
+ /// <reference path="../../../typings/unity.d.ts" />
2
+
1
3
import { UnityContext , UnityLoaderConfig } from '../context' ;
2
4
3
5
describe ( 'UnityContext' , ( ) => {
@@ -15,9 +17,95 @@ describe('UnityContext', () => {
15
17
productVersion : 'k' ,
16
18
} ;
17
19
20
+ beforeEach ( async ( ) => {
21
+ // @ts -ignore
22
+ delete window . __UnityBridgeRegistry__ ;
23
+ // @ts -ignore
24
+ delete window . UnityBridge ;
25
+ } ) ;
26
+
27
+ afterEach ( ( ) => {
28
+ jest . resetAllMocks ( ) ;
29
+ } ) ;
30
+
18
31
it ( 'stores and retrieves the loader configuration' , async ( ) => {
19
32
const ctx = new UnityContext ( cfg ) ;
20
33
21
- expect ( ctx . getConfig ( ) ) . toEqual ( cfg ) ;
34
+ expect ( ctx . getConfig ( ) ) . toStrictEqual ( cfg ) ;
35
+ } ) ;
36
+
37
+ it ( 'creates the global event registry' , async ( ) => {
38
+ expect ( window . __UnityBridgeRegistry__ ) . toBe ( undefined ) ;
39
+
40
+ new UnityContext ( cfg ) ;
41
+
42
+ expect ( window . __UnityBridgeRegistry__ ) . toStrictEqual ( { } ) ;
43
+ } ) ;
44
+
45
+ it ( 'creates the global event lookup handler' , async ( ) => {
46
+ expect ( window . UnityBridge ) . toBe ( undefined ) ;
47
+
48
+ new UnityContext ( cfg ) ;
49
+
50
+ expect ( typeof window . UnityBridge ) . toBe ( 'function' ) ;
51
+ } ) ;
52
+
53
+ it ( 'registers an event handler to the global registry' , async ( ) => {
54
+ const ctx = new UnityContext ( cfg ) ;
55
+ const handler = jest . fn ( ) ;
56
+
57
+ ctx . on ( 'test' , handler ) ;
58
+ expect ( window . __UnityBridgeRegistry__ ) . toStrictEqual ( { test : [ handler ] } ) ;
59
+ expect ( handler ) . not . toHaveBeenCalled ( ) ;
60
+
61
+ window . UnityBridge ( 'test' ) ( 'string' , 42 ) ;
62
+ expect ( handler ) . toHaveBeenCalledWith ( 'string' , 42 ) ;
63
+ } ) ;
64
+
65
+ it ( 'logs a warning when calling an unknown event' , async ( ) => {
66
+ new UnityContext ( cfg ) ;
67
+
68
+ let message : string = '' ;
69
+ const consoleWarn = jest
70
+ . spyOn ( console , 'warn' )
71
+ . mockImplementation ( ( m : string ) => ( message = m ) ) ;
72
+
73
+ expect ( consoleWarn ) . not . toHaveBeenCalled ( ) ;
74
+ window . UnityBridge ( 'test' ) ( 42 , test ) ;
75
+
76
+ expect ( consoleWarn ) . toHaveBeenCalled ( ) ;
77
+ expect ( message ) . toMatch ( / \" t e s t \" / ) ;
78
+ } ) ;
79
+
80
+ it ( 'unregisters a previously registered event' , async ( ) => {
81
+ const ctx = new UnityContext ( cfg ) ;
82
+ expect ( window . __UnityBridgeRegistry__ ) . toStrictEqual ( { } ) ;
83
+
84
+ // add handler
85
+ const handler = jest . fn ( ) ;
86
+ ctx . on ( 'test' , handler ) ;
87
+ expect ( window . __UnityBridgeRegistry__ ) . toStrictEqual ( { test : [ handler ] } ) ;
88
+
89
+ // remove it again
90
+ ctx . off ( 'test' ) ;
91
+ expect ( window . __UnityBridgeRegistry__ ) . toStrictEqual ( { test : [ ] } ) ;
92
+ } ) ;
93
+
94
+ it ( 'registers events with the same name for to contexts' , async ( ) => {
95
+ const ctxA = new UnityContext ( cfg ) ;
96
+ const ctxB = new UnityContext ( cfg ) ;
97
+
98
+ const handlerA = jest . fn ( ) ;
99
+ const handlerB = jest . fn ( ) ;
100
+ ctxA . on ( 'test-a-b' , handlerA ) ;
101
+ ctxB . on ( 'test-a-b' , handlerB ) ;
102
+
103
+ window . UnityBridge ( 'test-a-b' ) ( 'abtest' , 42 ) ;
104
+ expect ( handlerA ) . toHaveBeenCalledWith ( 'abtest' , 42 ) ;
105
+ expect ( handlerB ) . toHaveBeenCalledWith ( 'abtest' , 42 ) ;
106
+
107
+ expect ( window . __UnityBridgeRegistry__ ) . toStrictEqual ( {
108
+ 'test-a-b' : [ handlerA , handlerB ] , // order matters here!
109
+ } ) ;
22
110
} ) ;
23
111
} ) ;
0 commit comments