summaryrefslogtreecommitdiff
blob: 66e259c6987f3ae12a7d705bdcc478e9a287894c (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
( function ( mw ) {
	QUnit.module( 'ext.echo.dm - mw.echo.dm.BundleNotificationItem' );

	QUnit.test( 'Constructing the model', function ( assert ) {
		var bundledItems = [
				new mw.echo.dm.NotificationItem( 0, { read: false, seen: false, timestamp: '201601010000' } ),
				new mw.echo.dm.NotificationItem( 1, { read: false, seen: false, timestamp: '201601010100' } ),
				new mw.echo.dm.NotificationItem( 2, { read: false, seen: true, timestamp: '201601010200' } ),
				new mw.echo.dm.NotificationItem( 3, { read: false, seen: true, timestamp: '201601010300' } ),
				new mw.echo.dm.NotificationItem( 4, { read: false, seen: true, timestamp: '201601010400' } )
			],
			bundle = new mw.echo.dm.BundleNotificationItem(
				100,
				bundledItems,
				{ modelName: 'foo' }
			);

		assert.equal(
			bundle.getCount(),
			5,
			'Bundled items added to internal list'
		);

		assert.equal(
			bundle.getName(),
			'foo',
			'Bundle name stored'
		);

		assert.deepEqual(
			bundle.getAllIds(),
			[ 4, 3, 2, 1, 0 ],
			'All ids present'
		);

		assert.equal(
			bundle.isRead(),
			false,
			'Bundle with all unread items is unread'
		);

		assert.equal(
			bundle.hasUnseen(),
			true,
			'Bundle has unseen items'
		);

		assert.deepEqual(
			( function () {
				var findItems = bundle.findByIds( [ 1, 4 ] );
				return findItems.map( function ( item ) {
					return item.getId();
				} );
			}() ),
			[ 4, 1 ],
			'findByIds fetches correct items in the default sorting order'
		);
	} );

	QUnit.test( 'Managing a list of items', function ( assert ) {
		var i,
			bundledItems = [
				new mw.echo.dm.NotificationItem( 0, { read: false, seen: false, timestamp: '201601010000' } ),
				new mw.echo.dm.NotificationItem( 1, { read: false, seen: false, timestamp: '201601010100' } ),
				new mw.echo.dm.NotificationItem( 2, { read: false, seen: true, timestamp: '201601010200' } ),
				new mw.echo.dm.NotificationItem( 3, { read: false, seen: true, timestamp: '201601010300' } ),
				new mw.echo.dm.NotificationItem( 4, { read: false, seen: true, timestamp: '201601010400' } )
			],
			bundle = new mw.echo.dm.BundleNotificationItem(
				100,
				bundledItems,
				{
					name: 'foo'
				}
			);

		assert.equal(
			bundle.hasUnseen(),
			true,
			'Bundle has unseen'
		);

		// Mark all items as seen
		for ( i = 0; i < bundledItems.length; i++ ) {
			bundledItems[ i ].toggleSeen( true );
		}

		assert.equal(
			bundle.hasUnseen(),
			false,
			'Bundle does not have unseen after all items marked as seen'
		);

		assert.equal(
			bundle.isRead(),
			false,
			'Bundle is unread'
		);
		// Mark one item as read
		bundledItems[ 0 ].toggleRead( true );
		assert.equal(
			bundle.isRead(),
			false,
			'Bundle is still unread if it has some unread items'
		);

		// Mark all items as read
		for ( i = 0; i < bundledItems.length; i++ ) {
			bundledItems[ i ].toggleRead( true );
		}
		assert.equal(
			bundle.isRead(),
			true,
			'Bundle is marked as read if all items are read'
		);
	} );
}( mediaWiki ) );