Debian Codegraph @

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

raw symbols SCIP
1 QUnit.module( 'ext.echo.dm - NotificationItem', ( hooks ) => {
2 const fakeData = {
3 type: 'alert',
4 read: true,
5 seen: true,
6 timestamp: '2016-09-14T23:21:56Z',
7 content: {
8 header: 'Your edit on <strong>Moai</strong> was reverted.',
9 compactHeader: 'Your edit on <strong>Moai</strong> was reverted.',
10 body: 'undo'
11 },
12 iconType: 'revert',
13 primaryUrl: 'http://dev.wiki.local.wmftest.net:8080/w/index.php?title=Moai&oldid=prev&diff=1978&markasread=2126',
14 secondaryUrls: [
15 {
16 url: 'http://dev.wiki.local.wmftest.net:8080/wiki/User:RandomUser',
17 label: 'RandomUser',
18 icon: 'userAvatar'
19 },
20 {
21 url: 'http://dev.wiki.local.wmftest.net:8080/wiki/Talk:Moai',
22 label: 'Moai',
23 tooltip: 'Talk:Moai',
24 icon: 'speechBubbles'
25 }
26 ]
27 };
28 const now = 1234567890000;
29 const nowFormatted = '2009-02-13T23:31:30Z';
30
31 hooks.beforeEach( function () {
32 this.sandbox.useFakeTimers( now );
33 } );
34
35 QUnit.test.each( 'Constructing items', {
36 'Empty data': {
37 params: { id: 0, config: {} },
38 methods: 'all',
39 expected: { getId: 0, getAllIds: [ 0 ] }
40 },
41 'Fake data': {
42 params: { id: 999, config: fakeData },
43 methods: 'all',
44 expected: {
45 getId: 999,
46 getAllIds: [ 999 ],
47 getType: 'alert',
48 isRead: true,
49 isSeen: true,
50 getTimestamp: '2016-09-14T23:21:56Z',
51 getContentHeader: 'Your edit on <strong>Moai</strong> was reverted.',
52 getContentBody: 'undo',
53 getIconType: 'revert',
54 getPrimaryUrl: 'http://dev.wiki.local.wmftest.net:8080/w/index.php?title=Moai&oldid=prev&diff=1978&markasread=2126',
55 getSecondaryUrls: [
56 {
57 url: 'http://dev.wiki.local.wmftest.net:8080/wiki/User:RandomUser',
58 label: 'RandomUser',
59 icon: 'userAvatar'
60 },
61 {
62 url: 'http://dev.wiki.local.wmftest.net:8080/wiki/Talk:Moai',
63 label: 'Moai',
64 tooltip: 'Talk:Moai',
65 icon: 'speechBubbles'
66 }
67 ]
68 }
69 }
70 }, ( assert, data ) => {
71 const defaultValues = {
72 getId: undefined,
73 getContentHeader: '',
74 getContentBody: '',
75 getCategory: '',
76 getType: 'message',
77 isRead: false,
78 isSeen: false,
79 isForeign: false,
80 isBundled: false,
81 getTimestamp: nowFormatted,
82 getPrimaryUrl: undefined,
83 getIconUrl: undefined,
84 getIconType: undefined,
85 getSecondaryUrls: [],
86 getModelName: 'local',
87 getAllIds: []
88 };
89 const expected = $.extend( true, {}, defaultValues, data.expected );
90
91 const itemModel = new mw.echo.dm.NotificationItem(
92 data.params.id,
93 data.params.config
94 );
95
96 const methods = ( data.methods === 'all' ? Object.keys( expected ) : data.methods );
97 methods.forEach( ( method ) => {
98 assert.deepEqual(
99 // Run the method
100 itemModel[ method ](),
101 // Expected result
102 expected[ method ],
103 // Message
104 method
105 );
106 } );
107 } );
108
109 QUnit.test( 'Emitting update event', ( assert ) => {
110 const results = [];
111 const itemModel = new mw.echo.dm.NotificationItem( 0, $.extend( true, {}, fakeData, { seen: false, read: false } ) );
112
113 // Listen to update event
114 itemModel.on( 'update', () => {
115 results.push( [
116 itemModel.isRead(),
117 itemModel.isSeen()
118 ] );
119 } );
120
121 // Trigger events
122 itemModel.toggleSeen( true ); // [ [ false, true ] ]
123 itemModel.toggleSeen( true ); // [ [ false, true ] ] ( No change, event was not emitted )
124 itemModel.toggleRead( true ); // [ [ false, true ], [ true, true ] ]
125 itemModel.toggleRead( true ); // [ [ false, true ], [ true, true ] ] ( No change, event was not emitted )
126 itemModel.toggleRead( false ); // [ [ false, true ], [ true, true ], [ false, true ] ]
127 itemModel.toggleSeen( false ); // [ [ false, true ], [ true, true ], [ false, true ], [ false, false ] ]
128 itemModel.toggleRead( true ); // [ [ false, true ], [ true, true ], [ false, true ], [ false, false ], [ true, false ] ]
129
130 assert.deepEqual(
131 results,
132 // Expected:
133 [ [ false, true ], [ true, true ], [ false, true ], [ false, false ], [ true, false ] ],
134 'Read and seen changes produced "update" events'
135 );
136 } );
137
138 } );