Issues (16)

test/flash_test.js (3 issues)

1
describe('Unit testing Angular Flash', function() {
2
    var $compile,
3
        $rootScope,
4
        $interval,
5
        node,
6
        Flash;
7
8
    beforeEach(module('rv2Flash'));
9
10
    beforeEach(inject(function(_$compile_, _$rootScope_, _$interval_, _Flash_) {
11
        $compile = _$compile_;
12
        $rootScope = _$rootScope_;
13
        $interval = _$interval_;
14
        Flash = _Flash_;
15
    }));
16
17
    beforeEach(function() {
18
        node = $compile('<div><flash-message duration=1000></flash-message></div>')($rootScope);
19
    });
20
21
    it('replaces the element with the appropriate content', function() {
22
        var contents = node.contents();
23
        expect(contents[0].nodeType).toEqual(Node.ELEMENT_NODE);
0 ignored issues
show
The variable Node seems to be never declared. If this is a global, consider adding a /** global: Node */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
24
    });
25
26
    it('avoid to display flash when it does not have content', function() {
27
        created = Flash.create('success', '');
0 ignored issues
show
The variable created seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.created.
Loading history...
28
        $rootScope.$digest();
29
        var contents = node.contents()[0];
30
        expect(contents.querySelectorAll('.alert').length).toEqual(0);
31
        expect(created).toEqual(false);
32
    });
33
34
    it('has the class specified', function() {
35
        var testClassName = 'test-class';
36
        Flash.create('success', 'Good job', 10000, {class: testClassName});
37
        $rootScope.$digest();
38
        var contents = node.contents()[0];
39
        expect(contents.querySelectorAll('.alert')[0].classList).toContain(testClassName);
40
    });
41
42
    it('has the id specified', function() {
43
        var testIdName = 'test-id';
44
        Flash.create('success', 'Good job', 10000, {id: testIdName});
45
        $rootScope.$digest();
46
        var contents = node.contents()[0];
47
        expect(contents.querySelectorAll('.alert')[0].id).toContain(testIdName);
48
    });
49
50
    it('shows the flash when created and removes when deleted', function() {
51
        Flash.create('success', 'All good');
52
        $rootScope.$digest();
53
        var contents = node.contents()[0];
54
        expect(contents.querySelectorAll('.alert').length).toEqual(1);
55
        Flash.dismiss(0);
56
        $rootScope.$digest();
57
        expect(contents.querySelectorAll('.alert').length).toEqual(0);
58
    });
59
60
    it('clears all when clear is called', function() {
61
        for (var i = 0; i < 10; ++i) {
62
            Flash.create('success', 'All good');
63
        }
64
        $rootScope.$digest();
65
        var contents = node.contents()[0];
66
        expect(contents.querySelectorAll('.alert').length).toEqual(10);
67
        Flash.clear();
68
        $rootScope.$digest();
69
        expect(contents.querySelectorAll('.alert').length).toEqual(0);
70
    });
71
72
    it('is dismissed after timeout', function() {
73
        Flash.create('success', 'All good', 10000);
74
        $rootScope.$digest();
75
        var contents = node.contents()[0];
76
        $interval.flush(3000);
77
        expect(contents.querySelectorAll('.alert').length).toEqual(1);
78
79
        $interval.flush(10000);
80
        $rootScope.$digest();
81
        expect(contents.querySelectorAll('.alert').length).toEqual(0);
82
    });
83
84
    describe('show flashes in designated containers', function() {
85
        var containers;
86
87
        beforeEach(function() {
88
            containers = $compile(
89
                '<flash-message duration=1000></flash-message>' +
90
                '<flash-message duration=1000 name="flash-container-a"></flash-message>' +
91
                '<flash-message duration=1000 name="flash-container-b"></flash-message>')($rootScope);
92
93
            Flash.create('success', 'All good');
94
            Flash.create('success', 'All good - A', 0, { container: 'flash-container-a'});
95
            Flash.create('success', 'All good - B', 0, { container: 'flash-container-b'});
96
97
            $rootScope.$digest();
98
        });
99
100
        it('only shows default alert in default container', function() {
101
            expect(containers[0].querySelectorAll('.alert').length).toEqual(1);
102
            expect(containers[0].outerHTML).toContain('All good');
103
            expect(containers[0].outerHTML).not.toContain('All good - A');
104
            expect(containers[0].outerHTML).not.toContain('All good - B');
105
        });
106
107
        it('only shows alert A in container A', function() {
108
            expect(containers[1].querySelectorAll('.alert').length).toEqual(1);
109
            expect(containers[1].outerHTML).toContain('All good - A');
110
            expect(containers[1].outerHTML).not.toContain('All good - B');
111
        });
112
113
        it('only shows alert B in container B', function() {
114
            expect(containers[2].querySelectorAll('.alert').length).toEqual(1);
115
            expect(containers[2].outerHTML).toContain('All good - B');
116
            expect(containers[2].outerHTML).not.toContain('All good - A');
117
        });
118
    });
119
120
    describe('close button', function () {
121
        it('is shown by default', function() {
122
            Flash.create('success', 'All good');
123
            $rootScope.$digest();
124
            var contents = node.contents()[0];
125
            expect(contents.querySelectorAll('.close')[0].classList.contains('ng-hide')).toEqual(false);
126
        });
127
128
        it('can be hidden with directive parameter', function() {
129
            node = $compile('<div><flash-message show-close="false"></flash-message></div>')($rootScope);
130
            Flash.create('success', 'All good');
131
            $rootScope.$digest();
132
            var contents = node.contents()[0];
133
            expect(contents.querySelectorAll('.close')[0].classList.contains('ng-hide')).toEqual(true);
134
        });
135
136
        it('can be hidden with create function parameter', function() {
137
            Flash.create('success', 'All good', 0, '', false);
138
            $rootScope.$digest();
139
            var contents = node.contents()[0];
140
            expect(contents.querySelectorAll('.close')[0].classList.contains('ng-hide')).toEqual(true);
141
        });
142
143
        it('can be overriden with create function parameter', function() {
144
            node = $compile('<div><flash-message show-close="false"></flash-message></div>')($rootScope);
145
            Flash.create('success', 'All good', 0, '', true);
146
            $rootScope.$digest();
147
            var contents = node.contents()[0];
148
            expect(contents.querySelectorAll('.close')[0].classList.contains('ng-hide')).toEqual(false);
149
        });
150
    });
151
152
    describe('dismiss callback', function() {
153
        it('is called when dismissed', function() {
154
            $rootScope.myCallback = function(flash) {
0 ignored issues
show
The parameter flash is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
155
            };
156
            spyOn($rootScope, "myCallback");
157
            node = $compile('<div><flash-message on-dismiss="myCallback(flash)"></flash-message></div>')($rootScope);
158
            const id = Flash.create('success', 'All good');
159
            Flash.dismiss(id);
160
            $rootScope.$digest();
161
            expect($rootScope.myCallback).toHaveBeenCalled();
162
        });
163
    })
164
});
165