Debian Codegraph @

extensions/Echo/tests/qunit/model/test_mw.echo.dm.NotificationGroupsList.js

raw symbols SCIP
1 QUnit.module( 'ext.echo.dm - NotificationGroupsList' );
2
3 QUnit.test( 'Constructing the model', ( assert ) => {
4 const model = new mw.echo.dm.NotificationGroupsList();
5
6 assert.strictEqual(
7 model.getTimestamp(),
8 0,
9 'Empty group has timestamp 0'
10 );
11 } );
12
13 QUnit.test( 'Managing lists', ( assert ) => {
14 const model = new mw.echo.dm.NotificationGroupsList();
15 const groupDefinitions = [
16 {
17 name: 'foo',
18 sourceData: {
19 title: 'Foo Wiki',
20 base: 'http://foo.wiki.sample/$1'
21 },
22 items: [
23 new mw.echo.dm.NotificationItem( 0 ),
24 new mw.echo.dm.NotificationItem( 1 ),
25 new mw.echo.dm.NotificationItem( 2 )
26 ]
27 },
28 {
29 name: 'bar',
30 sourceData: {
31 title: 'Bar Wiki',
32 base: 'http://bar.wiki.sample/$1'
33 },
34 items: [
35 new mw.echo.dm.NotificationItem( 3 ),
36 new mw.echo.dm.NotificationItem( 4 ),
37 new mw.echo.dm.NotificationItem( 5 ),
38 new mw.echo.dm.NotificationItem( 6 )
39 ]
40 },
41 {
42 name: 'baz',
43 sourceData: {
44 title: 'Baz Wiki',
45 base: 'http://baz.wiki.sample/$1'
46 },
47 items: [
48 new mw.echo.dm.NotificationItem( 7 )
49 ]
50 }
51 ];
52
53 groupDefinitions.forEach( ( def, i ) => {
54 model.addGroup(
55 def.name,
56 def.sourceData,
57 def.items
58 );
59
60 assert.strictEqual(
61 model.getItemCount(),
62 i + 1,
63 'Group number increases after addGroup ("' + def.name + '")'
64 );
65
66 const result = model.getGroupByName( def.name );
67 assert.strictEqual(
68 result.getName(),
69 def.name,
70 'Group exists after addGroup ("' + def.name + '")'
71 );
72 } );
73
74 // Remove group
75 model.removeGroup( groupDefinitions[ 0 ].name );
76
77 assert.strictEqual(
78 model.getItemCount(),
79 groupDefinitions.length - 1,
80 'Group number decreased after removeGroup'
81 );
82 assert.strictEqual(
83 model.getGroupByName( groupDefinitions[ 0 ] ),
84 null,
85 'Removed group is no longer in the list'
86 );
87
88 // Removing the last item from a group should remove the group
89 const group = model.getGroupByName( 'baz' );
90 group.discardItems( groupDefinitions[ 2 ].items );
91 assert.strictEqual(
92 model.getGroupByName( 'baz' ),
93 null,
94 'Empty group is no longer in the list'
95 );
96 } );
97
98 QUnit.test( 'Emitting discard event', ( assert ) => {
99 const results = [];
100 const model = new mw.echo.dm.NotificationGroupsList();
101 const groups = {
102 first: [
103 new mw.echo.dm.NotificationItem( 0 ),
104 new mw.echo.dm.NotificationItem( 1 ),
105 new mw.echo.dm.NotificationItem( 2 )
106 ],
107 second: [
108 new mw.echo.dm.NotificationItem( 3 ),
109 new mw.echo.dm.NotificationItem( 4 ),
110 new mw.echo.dm.NotificationItem( 5 )
111 ],
112 third: [
113 new mw.echo.dm.NotificationItem( 6 ),
114 new mw.echo.dm.NotificationItem( 7 )
115 ],
116 fourth: [
117 new mw.echo.dm.NotificationItem( 8 ),
118 new mw.echo.dm.NotificationItem( 9 )
119 ]
120 };
121
122 // Listen to the event
123 model
124 .on( 'discard', ( g ) => {
125 results.push( g.getName() );
126 } );
127
128 // Fill the list
129 for ( const group in groups ) {
130 model.addGroup( group, {}, groups[ group ] );
131 }
132
133 // Trigger events
134 model.removeGroup( 'first' ); // [ 'first' ]
135 model.removeGroup( 'fourth' ); // [ 'first', 'fourth' ]
136 // Group doesn't exist, no change
137 model.removeGroup( 'first' ); // [ 'first', 'fourth' ]
138 // Discard of an item in a group (no event on the list model)
139 model.getGroupByName( 'third' ).discardItems( groups.third[ 0 ] ); // [ 'first', 'fourth' ]
140 // Discard of the last item in a group (trigger discard event on the list model)
141 model.getGroupByName( 'third' ).discardItems( groups.third[ 1 ] ); // [ 'first', 'fourth', 'third' ]
142
143 assert.deepEqual(
144 // Actual
145 results,
146 // Expected:
147 [ 'first', 'fourth', 'third' ],
148 // Message
149 'Discard events emitted'
150 );
151 } );