GUI -- ThemeService: Unit tests for add/remove listeners.
Change-Id: I427398fa02c9d4b7c7dd892872a02e6faecf9121
Showing
1 changed file
with
66 additions
and
1 deletions
| ... | @@ -92,6 +92,71 @@ describe('factory: fw/util/theme.js', function() { | ... | @@ -92,6 +92,71 @@ describe('factory: fw/util/theme.js', function() { |
| 92 | }); | 92 | }); |
| 93 | 93 | ||
| 94 | 94 | ||
| 95 | - // TODO : Unit tests for listeners... | 95 | + // === Unit Tests for listeners |
| 96 | + | ||
| 97 | + it('should report lack of callback', function () { | ||
| 98 | + spyOn($log, 'error'); | ||
| 99 | + var list = ts.addListener(); | ||
| 100 | + expect($log.error).toHaveBeenCalledWith( | ||
| 101 | + 'ThemeService.addListener(): callback not a function' | ||
| 102 | + ); | ||
| 103 | + expect(list.error).toEqual('No callback defined'); | ||
| 104 | + }); | ||
| 105 | + | ||
| 106 | + it('should report non-functional callback', function () { | ||
| 107 | + spyOn($log, 'error'); | ||
| 108 | + var list = ts.addListener(['some array']); | ||
| 109 | + expect($log.error).toHaveBeenCalledWith( | ||
| 110 | + 'ThemeService.addListener(): callback not a function' | ||
| 111 | + ); | ||
| 112 | + expect(list.error).toEqual('No callback defined'); | ||
| 113 | + }); | ||
| 114 | + | ||
| 115 | + it('should invoke our callback with an event', function () { | ||
| 116 | + var event; | ||
| 117 | + | ||
| 118 | + function cb(ev) { | ||
| 119 | + event = ev; | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + expect(event).toBeUndefined(); | ||
| 123 | + ts.addListener(cb); | ||
| 124 | + ts.theme('dark'); | ||
| 125 | + expect(event).toEqual({ | ||
| 126 | + event: 'themeChange', | ||
| 127 | + value: 'dark' | ||
| 128 | + }); | ||
| 129 | + }); | ||
| 130 | + | ||
| 131 | + it('should invoke our callback at appropriate times', function () { | ||
| 132 | + var calls = [], | ||
| 133 | + phase, | ||
| 134 | + listener; | ||
| 135 | + | ||
| 136 | + function cb() { | ||
| 137 | + calls.push(phase); | ||
| 138 | + } | ||
| 139 | + | ||
| 140 | + expect(calls).toEqual([]); | ||
| 141 | + | ||
| 142 | + phase = 'pre'; | ||
| 143 | + ts.toggleTheme(); // -> dark | ||
| 144 | + | ||
| 145 | + phase = 'added'; | ||
| 146 | + listener = ts.addListener(cb); | ||
| 147 | + ts.toggleTheme(); // -> light | ||
| 148 | + | ||
| 149 | + phase = 'same'; | ||
| 150 | + ts.theme('light'); // (still light - no event) | ||
| 151 | + | ||
| 152 | + phase = 'diff'; | ||
| 153 | + ts.theme('dark'); // -> dark | ||
| 154 | + | ||
| 155 | + phase = 'post'; | ||
| 156 | + ts.removeListener(listener); | ||
| 157 | + ts.toggleTheme(); // -> light | ||
| 158 | + | ||
| 159 | + expect(calls).toEqual(['added', 'diff']); | ||
| 160 | + }); | ||
| 96 | 161 | ||
| 97 | }); | 162 | }); | ... | ... |
-
Please register or login to post a comment