summaryrefslogtreecommitdiff
blob: c71845a5a0f7c351029e5cb4f3a5b80ca20baa9f (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
( function () {
	QUnit.module( 'ext.echo.dm - mw.echo.dm.NotificationGroupsList' );

	QUnit.test( 'Constructing the model', function ( assert ) {
		var model = new mw.echo.dm.NotificationGroupsList();

		assert.strictEqual(
			model.getTimestamp(),
			0,
			'Empty group has timestamp 0'
		);
	} );

	QUnit.test( 'Managing lists', function ( assert ) {
		var i, group,
			model = new mw.echo.dm.NotificationGroupsList(),
			groupDefinitions = [
				{
					name: 'foo',
					sourceData: {
						title: 'Foo Wiki',
						base: 'http://foo.wiki.sample/$1'
					},
					items: [
						new mw.echo.dm.NotificationItem( 0 ),
						new mw.echo.dm.NotificationItem( 1 ),
						new mw.echo.dm.NotificationItem( 2 )
					]
				},
				{
					name: 'bar',
					sourceData: {
						title: 'Bar Wiki',
						base: 'http://bar.wiki.sample/$1'
					},
					items: [
						new mw.echo.dm.NotificationItem( 3 ),
						new mw.echo.dm.NotificationItem( 4 ),
						new mw.echo.dm.NotificationItem( 5 ),
						new mw.echo.dm.NotificationItem( 6 )
					]
				},
				{
					name: 'baz',
					sourceData: {
						title: 'Baz Wiki',
						base: 'http://baz.wiki.sample/$1'
					},
					items: [
						new mw.echo.dm.NotificationItem( 7 )
					]
				}
			];

		for ( i = 0; i < groupDefinitions.length; i++ ) {
			model.addGroup(
				groupDefinitions[ i ].name,
				groupDefinitions[ i ].sourceData,
				groupDefinitions[ i ].items
			);

			assert.strictEqual(
				model.getItemCount(),
				i + 1,
				'Group number increases after addGroup ("' + groupDefinitions[ i ].name + '")'
			);

			group = model.getGroupByName( groupDefinitions[ i ].name );
			assert.strictEqual(
				group.getName(),
				groupDefinitions[ i ].name,
				'Group exists after addGroup ("' + groupDefinitions[ i ].name + '")'
			);
		}

		// Remove group
		model.removeGroup( groupDefinitions[ 0 ].name );

		assert.strictEqual(
			model.getItemCount(),
			groupDefinitions.length - 1,
			'Group number decreased after removeGroup'
		);
		assert.strictEqual(
			model.getGroupByName( groupDefinitions[ 0 ] ),
			null,
			'Removed group is no longer in the list'
		);

		// Removing the last item from a group should remove the group
		group = model.getGroupByName( 'baz' );
		group.discardItems( groupDefinitions[ 2 ].items );
		assert.strictEqual(
			model.getGroupByName( 'baz' ),
			null,
			'Empty group is no longer in the list'
		);
	} );

	QUnit.test( 'Emitting discard event', function ( assert ) {
		var group,
			results = [],
			model = new mw.echo.dm.NotificationGroupsList(),
			groups = {
				first: [
					new mw.echo.dm.NotificationItem( 0 ),
					new mw.echo.dm.NotificationItem( 1 ),
					new mw.echo.dm.NotificationItem( 2 )
				],
				second: [
					new mw.echo.dm.NotificationItem( 3 ),
					new mw.echo.dm.NotificationItem( 4 ),
					new mw.echo.dm.NotificationItem( 5 )
				],
				third: [
					new mw.echo.dm.NotificationItem( 6 ),
					new mw.echo.dm.NotificationItem( 7 )
				],
				fourth: [
					new mw.echo.dm.NotificationItem( 8 ),
					new mw.echo.dm.NotificationItem( 9 )
				]
			};

		// Listen to the event
		model
			.on( 'discard', function ( group ) {
				results.push( group.getName() );
			} );

		// Fill the list
		for ( group in groups ) {
			model.addGroup( group, {}, groups[ group ] );
		}

		// Trigger events
		model.removeGroup( 'first' ); // [ 'first' ]
		model.removeGroup( 'fourth' ); // [ 'first', 'fourth' ]
		// Group doesn't exist, no change
		model.removeGroup( 'first' ); // [ 'first', 'fourth' ]
		// Discard of an item in a group (no event on the list model)
		model.getGroupByName( 'third' ).discardItems( groups.third[ 0 ] ); // [ 'first', 'fourth' ]
		// Discard of the last item in a group (trigger discard event on the list model)
		model.getGroupByName( 'third' ).discardItems( groups.third[ 1 ] ); // [ 'first', 'fourth', 'third' ]

		assert.deepEqual(
			// Actual
			results,
			// Expected:
			[ 'first', 'fourth', 'third' ],
			// Message
			'Discard events emitted'
		);
	} );

}() );