1 | import {
|
2 | expect
|
3 | } from 'chai';
|
4 | import sinon from 'sinon';
|
5 | import PropTypes from 'proptypes';
|
6 |
|
7 | import setup from '../src/setup';
|
8 | import angular from 'angular';
|
9 | import 'angular-mocks';
|
10 |
|
11 | describe('Angular app setup', () => {
|
12 | let app;
|
13 | let $compile;
|
14 | let $rootScope;
|
15 |
|
16 | function testDirDefWithoutPropTypes() {
|
17 | return {
|
18 | scope: {},
|
19 | link($scope) {
|
20 | return $scope;
|
21 | },
|
22 | template: '<div>hello {{hello}}</div>',
|
23 | };
|
24 | }
|
25 |
|
26 | function testDirDefWithPropTypesExisted() {
|
27 | return {
|
28 | _propTypes_: {},
|
29 | link($scope) {
|
30 | return $scope;
|
31 | },
|
32 | template: '<div>hello {{hello}}</div>',
|
33 | };
|
34 | }
|
35 |
|
36 | function testDirDefWithPropTypesDefined() {
|
37 | return {
|
38 | _propTypes_: {
|
39 | hello: PropTypes.string,
|
40 | },
|
41 | scope: {
|
42 | hello: '=',
|
43 | },
|
44 | link($scope) {
|
45 | return $scope;
|
46 | },
|
47 | template: '<div>hello {{hello}}</div>',
|
48 | };
|
49 | }
|
50 |
|
51 | let initInject = false;
|
52 |
|
53 | function _initInject() {
|
54 | angular.mock.module('test');
|
55 | angular.mock.inject((_$compile_, _$rootScope_) => {
|
56 | $compile = _$compile_;
|
57 | $rootScope = _$rootScope_;
|
58 | });
|
59 | initInject = true;
|
60 | }
|
61 |
|
62 | function angularInject(html = '<test-dir/>') {
|
63 | !initInject && _initInject();
|
64 |
|
65 | const $nuScope = $rootScope.$new(true);
|
66 | const $element = $compile(html)($nuScope);
|
67 | $nuScope.$digest();
|
68 | return $element;
|
69 | }
|
70 |
|
71 |
|
72 | beforeEach(() => {
|
73 | app = angular.module('test', []);
|
74 | });
|
75 | afterEach(() => {
|
76 | initInject = false;
|
77 | })
|
78 |
|
79 | it('should setup correctly', () => {
|
80 | app = setup(app);
|
81 | angular.mock.module('test');
|
82 | expect(1).to.equal(1, 'no exception alone the way');
|
83 | });
|
84 |
|
85 | it('should should allow initial state', (done) => {
|
86 |
|
87 | app = setup(app);
|
88 | app.config(['ngStoreProvider', ngStore => {
|
89 | ngStore.setInitialState({
|
90 | hello: 'world'
|
91 | });
|
92 | ngStore.setReducers(state => {
|
93 | state.hello = state.hello + ' > ' + (new Date());
|
94 | return state;
|
95 | });
|
96 | }]);
|
97 | app.run(['ngStore', store => {
|
98 | expect(store.getState().hello).to.contain('world');
|
99 | done();
|
100 | }]);
|
101 | angularInject();
|
102 | });
|
103 |
|
104 | it('should warn directive without propTypes', () => {
|
105 | const warning = sinon.spy(console, 'warn');
|
106 | app = setup(app);
|
107 | app.directive('testDir', testDirDefWithoutPropTypes);
|
108 |
|
109 | angularInject();
|
110 |
|
111 | expect(warning.called).to.be.true;
|
112 | warning.restore();
|
113 | });
|
114 |
|
115 | it('should NOT warn directive with propTypes suggested', () => {
|
116 | const warning = sinon.spy(console, 'warn');
|
117 | app = setup(app);
|
118 | app.directive('testDir', testDirDefWithPropTypesExisted);
|
119 |
|
120 | angularInject();
|
121 |
|
122 | expect(warning.called).to.be.false;
|
123 | warning.restore();
|
124 | });
|
125 |
|
126 | it('should check directive propTypes', () => {
|
127 | const warning = sinon.spy(console, 'warn');
|
128 | app = setup(app);
|
129 | app.directive('testDir', testDirDefWithPropTypesDefined);
|
130 |
|
131 | expect(angularInject.bind(null, '<test-dir hello="1"/>')).to.throw(Error);
|
132 | expect(warning.called).to.be.true;
|
133 |
|
134 | expect(angularInject.bind(null, '<test-dir hello="world"/>')).not.to.throw(Error);
|
135 | warning.restore();
|
136 | });
|
137 | });
|