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
|
( function ( mw, $ ) {
var defaultValues = {
getReadState: 'all'
};
QUnit.module( 'ext.echo.dm - mw.echo.dm.FiltersModel' );
QUnit.test( 'Constructing the model', function ( assert ) {
var i, model, method,
cases = [
{
msg: 'Empty config',
config: {},
expected: defaultValues
},
{
msg: 'Readstate: unread',
config: {
readState: 'unread'
},
expected: $.extend( true, {}, defaultValues, {
getReadState: 'unread'
} )
},
{
msg: 'Readstate: read',
config: {
readState: 'read'
},
expected: $.extend( true, {}, defaultValues, {
getReadState: 'read'
} )
}
];
for ( i = 0; i < cases.length; i++ ) {
model = new mw.echo.dm.FiltersModel( cases[ i ].config );
for ( method in cases[ i ].expected ) {
assert.deepEqual(
// Run the method
model[ method ](),
// Expected value
cases[ i ].expected[ method ],
// Message
cases[ i ].msg + ' (' + method + ')'
);
}
}
} );
QUnit.test( 'Changing filters', function ( assert ) {
var model = new mw.echo.dm.FiltersModel();
assert.strictEqual(
model.getReadState(),
'all',
'Initial value: all'
);
model.setReadState( 'unread' );
assert.strictEqual(
model.getReadState(),
'unread',
'Changing state (unread)'
);
model.setReadState( 'read' );
assert.strictEqual(
model.getReadState(),
'read',
'Changing state (read)'
);
model.setReadState( 'foo' );
assert.strictEqual(
model.getReadState(),
'read',
'Ignoring invalid state (foo)'
);
} );
QUnit.test( 'Emitting update event', function ( assert ) {
var results = [],
model = new mw.echo.dm.FiltersModel();
// Listen to update event
model.on( 'update', function () {
results.push( model.getReadState() );
} );
// Trigger events
model.setReadState( 'read' ); // [ 'read' ]
model.setReadState( 'unread' ); // [ 'read', 'unread' ]
model.setReadState( 'unread' ); // (no change, no event) [ 'read', 'unread' ]
model.setReadState( 'all' ); // [ 'read', 'unread', 'all' ]
model.setReadState( 'foo' ); // (invalid value, no event) [ 'read', 'unread', 'all' ]
model.setReadState( 'unread' ); // [ 'read', 'unread', 'all', 'unread' ]
assert.deepEqual(
// Actual
results,
// Expected:
[ 'read', 'unread', 'all', 'unread' ],
// Message
'Update events emitted'
);
} );
}( mediaWiki, jQuery ) );
|