|
1
|
|
|
import {IObserver, IObserverAdapter} from '../Observer/Observer'; |
|
2
|
|
|
import RouterRegistry from '../Router/Registry'; |
|
3
|
|
|
import Router, {IPageData} from '../Router/Router'; |
|
4
|
|
|
import Action from './Action'; |
|
5
|
|
|
import {IModulePageData} from './Application'; |
|
6
|
|
|
import ModuleLoader from './ModuleLoader'; |
|
7
|
|
|
|
|
8
|
|
|
describe('Application.Action', () => { |
|
9
|
|
|
let menuOpenState: IObserver<boolean>, |
|
10
|
|
|
routerObserver: IObserver<IPageData | null>, |
|
11
|
|
|
router: Router, |
|
12
|
|
|
routerRegistry: RouterRegistry, |
|
13
|
|
|
routerAdapter: IObserverAdapter<IPageData | null>, |
|
14
|
|
|
moduleLoader: ModuleLoader |
|
15
|
|
|
; |
|
16
|
|
|
|
|
17
|
|
|
beforeEach(() => { |
|
18
|
|
|
menuOpenState = { |
|
19
|
|
|
value: false, |
|
20
|
|
|
adapter: {onChange: (newValue) => {}} |
|
21
|
|
|
}; |
|
22
|
|
|
router = jest.genMockFromModule<Router>('../Router/Router'); |
|
23
|
|
|
routerRegistry = jest.genMockFromModule<RouterRegistry>('../Router/Registry'); |
|
24
|
|
|
moduleLoader = jest.genMockFromModule<ModuleLoader>('./ModuleLoader'); |
|
25
|
|
|
routerAdapter = { |
|
26
|
|
|
onChange: jest.fn() |
|
27
|
|
|
}; |
|
28
|
|
|
routerObserver = { |
|
29
|
|
|
adapter: routerAdapter, |
|
30
|
|
|
value: null |
|
31
|
|
|
}; |
|
32
|
|
|
}); |
|
33
|
|
|
|
|
34
|
|
|
function createTestObject(): Action { |
|
35
|
|
|
return new Action( |
|
36
|
|
|
menuOpenState, |
|
37
|
|
|
routerObserver, |
|
38
|
|
|
router, |
|
39
|
|
|
routerRegistry, |
|
40
|
|
|
routerAdapter, |
|
41
|
|
|
moduleLoader |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
it('Open tab to github', () => { |
|
46
|
|
|
const action: Action = createTestObject(); |
|
47
|
|
|
|
|
48
|
|
|
window.open = jest.fn(); |
|
49
|
|
|
action.adapter.onGithubClick(); |
|
50
|
|
|
|
|
51
|
|
|
expect(window.open).toHaveBeenCalledWith('https://github.com/enbock/Time-Tracker/', '_blank'); |
|
52
|
|
|
}); |
|
53
|
|
|
|
|
54
|
|
|
it('Switch menu', () => { |
|
55
|
|
|
const action: Action = createTestObject(); |
|
56
|
|
|
|
|
57
|
|
|
action.adapter.onMenuClick(); |
|
58
|
|
|
|
|
59
|
|
|
expect(menuOpenState.value).toBe(true); |
|
60
|
|
|
}); |
|
61
|
|
|
|
|
62
|
|
|
it('Close menu', () => { |
|
63
|
|
|
menuOpenState.value = true; |
|
64
|
|
|
const action: Action = createTestObject(); |
|
65
|
|
|
|
|
66
|
|
|
action.adapter.onClose(); |
|
67
|
|
|
action.adapter.onClose(); |
|
68
|
|
|
|
|
69
|
|
|
expect(menuOpenState.value).toBe(false); |
|
70
|
|
|
}); |
|
71
|
|
|
|
|
72
|
|
|
it('Change page', () => { |
|
73
|
|
|
const action: Action = createTestObject(); |
|
74
|
|
|
const page: IPageData = { |
|
75
|
|
|
depth: 0, |
|
76
|
|
|
name: 'name', |
|
77
|
|
|
rootUrl: 'rootUrl', |
|
78
|
|
|
url: 'url' |
|
79
|
|
|
}; |
|
80
|
|
|
routerRegistry.getPages = jest.fn().mockReturnValue({name: page}); |
|
81
|
|
|
router.changePage = jest.fn(); |
|
82
|
|
|
menuOpenState.value = true; |
|
83
|
|
|
|
|
84
|
|
|
action.adapter.onMenu('name'); |
|
85
|
|
|
expect(router.changePage).toHaveBeenCalledWith(page); |
|
86
|
|
|
expect(menuOpenState.value).toBeFalsy(); |
|
87
|
|
|
}); |
|
88
|
|
|
|
|
89
|
|
|
it('Load module on page change', async () => { |
|
90
|
|
|
const action: Action = createTestObject(); |
|
91
|
|
|
const page: IModulePageData = { |
|
92
|
|
|
module: './New/Module', |
|
93
|
|
|
depth: 0, |
|
94
|
|
|
name: 'name', |
|
95
|
|
|
rootUrl: 'rootUrl', |
|
96
|
|
|
url: 'url' |
|
97
|
|
|
}; |
|
98
|
|
|
moduleLoader.loadModule = jest.fn().mockResolvedValue(undefined); |
|
99
|
|
|
|
|
100
|
|
|
await action.adapter.onPageChanged(page); |
|
101
|
|
|
expect(moduleLoader.loadModule).toHaveBeenCalledWith('./New/Module'); |
|
102
|
|
|
}); |
|
103
|
|
|
|
|
104
|
|
|
it('Load page config and initialize module loader with home page', () => { |
|
105
|
|
|
routerRegistry.registerPage = jest.fn(); |
|
106
|
|
|
router.changePage = jest.fn(); |
|
107
|
|
|
const action: Action = createTestObject(); |
|
108
|
|
|
action.loadPageConfig(); |
|
109
|
|
|
|
|
110
|
|
|
const homePage: { depth: number; module: string; name: string; url: string; rootUrl: string } = { |
|
111
|
|
|
depth: 0, |
|
112
|
|
|
name: 'home', |
|
113
|
|
|
rootUrl: './', |
|
114
|
|
|
url: './', |
|
115
|
|
|
module: './HelloWorld' |
|
116
|
|
|
}; |
|
117
|
|
|
expect(routerRegistry.registerPage).toBeCalledWith(homePage); |
|
118
|
|
|
expect(routerRegistry.registerPage).toBeCalledWith({ |
|
119
|
|
|
depth: 1, |
|
120
|
|
|
name: 'settings', |
|
121
|
|
|
rootUrl: './settings/', |
|
122
|
|
|
url: './settings/', |
|
123
|
|
|
module: './Settings/Settings' |
|
124
|
|
|
}); |
|
125
|
|
|
expect(router.changePage).toBeCalledWith(homePage); |
|
126
|
|
|
}); |
|
127
|
|
|
|
|
128
|
|
|
it('Load page config and initialize module loader with last page', () => { |
|
129
|
|
|
const homePage: { depth: number; module: string; name: string; url: string; rootUrl: string } = { |
|
130
|
|
|
depth: 0, |
|
131
|
|
|
name: 'home', |
|
132
|
|
|
rootUrl: './', |
|
133
|
|
|
url: './', |
|
134
|
|
|
module: './HelloWorld' |
|
135
|
|
|
}; |
|
136
|
|
|
const lastPage: { depth: number; module: string; name: string; url: string; rootUrl: string } = { |
|
137
|
|
|
depth: 1, |
|
138
|
|
|
name: 'settings', |
|
139
|
|
|
rootUrl: './settings/', |
|
140
|
|
|
url: './settings/', |
|
141
|
|
|
module: './Settings/Settings' |
|
142
|
|
|
}; |
|
143
|
|
|
|
|
144
|
|
|
routerRegistry.registerPage = jest.fn(); |
|
145
|
|
|
router.changePage = jest.fn(); |
|
146
|
|
|
routerAdapter.onChange = jest.fn(); |
|
147
|
|
|
routerObserver.value = lastPage; |
|
148
|
|
|
|
|
149
|
|
|
const action: Action = createTestObject(); |
|
150
|
|
|
action.loadPageConfig(); |
|
151
|
|
|
|
|
152
|
|
|
expect(routerRegistry.registerPage).toBeCalledWith(homePage); |
|
153
|
|
|
expect(routerRegistry.registerPage).toBeCalledWith(lastPage); |
|
154
|
|
|
expect(router.changePage).not.toHaveBeenCalled(); |
|
155
|
|
|
expect(routerAdapter.onChange).toHaveBeenCalledWith(lastPage); |
|
156
|
|
|
}); |
|
157
|
|
|
}); |
|
158
|
|
|
|