-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.ts
76 lines (74 loc) · 2.73 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { bootstrap, element as $, module } from 'angular'
import 'angular-mocks'
import { $http, $httpBackend, $log, $rootScope } from './'
describe('ngimport', () => {
afterEach(() => {
$http.defaults.headers.common = {}
})
it('should not define angular builtins before the app is bootstrapped', () => {
const element = div()
module('a', ['bcherny/ngimport'])
expect($http).toBeUndefined()
expect($rootScope).toBeUndefined()
bootstrap(element, ['a'])
element.remove()
})
it('should define angular builtins after the app is bootstrapped', () => {
const element = div()
module('a', ['bcherny/ngimport'])
bootstrap(element, ['a'])
expect($http).toBeDefined()
expect(typeof $http.get).toBe('function')
expect(typeof $rootScope.$id).toBe('number')
element.remove()
})
it('should not override ngimported provider state', () => {
const element = div()
module('a', ['bcherny/ngimport'])
bootstrap(element, ['a'])
$http.defaults.headers.common.Authorization = 'Basic YmVlcDpib29w'
module('a').run(() => {
expect($http.defaults.headers.common.Authorization).toBe('Basic YmVlcDpib29w')
})
element.remove()
})
it('should share ngimported service state between ngimported and injected instances', () => {
const element = div()
module('a', ['bcherny/ngimport'])
const $injector = bootstrap(element, ['a'])
$http.defaults.headers.common.Authorization = 'Basic YmVlcDpib29w'
expect($injector.get('$http').defaults.headers.common.Authorization).toBe('Basic YmVlcDpib29w')
element.remove()
})
it('should share ngimported service state between injected and ngimported instances', () => {
const element = div()
module('a', ['bcherny/ngimport'])
const $injector = bootstrap(element, ['a'])
$injector.get('$http').defaults.headers.common.Authorization = 'Basic YmVlcDpib29w'
expect($http.defaults.headers.common.Authorization).toBe('Basic YmVlcDpib29w')
element.remove()
})
it('should be able to mock dependencies with $provide', () => {
const element = div()
module('a', ['bcherny/ngimport'])
.constant('$log', 42)
const $injector = bootstrap(element, ['a'])
expect($injector.get('$log') as any).toBe(42)
expect($log as any).toBe(42)
element.remove()
})
it('should be able to assert against HTTP requests with $httpBackend', () => {
const element = div()
module('a', ['ngMock', 'bcherny/ngimport'])
bootstrap(element, ['a'])
$httpBackend.expectGET('/url').respond(null)
$http.get('/url')
$httpBackend.flush()
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()
element.remove()
})
})
function div() {
return $(document.createElement('div'))
}