-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathvalue-accessor-tests.spec.ts
More file actions
165 lines (129 loc) · 5.17 KB
/
value-accessor-tests.spec.ts
File metadata and controls
165 lines (129 loc) · 5.17 KB
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
157
158
159
160
161
162
163
164
165
// make sure you import mocha-config before @angular/core
import { ElementRef } from '@angular/core';
import {
CheckedValueAccessor,
DateValueAccessor,
NumberValueAccessor,
SelectedIndexValueAccessor,
TextValueAccessor,
TimeValueAccessor,
} from '@nativescript/angular';
import { DatePicker, ListPicker, Slider, Switch, TextField, TimePicker, View } from '@nativescript/core';
class TestElementRef implements ElementRef {
constructor(public nativeElement: View) {}
}
class TestNumberValueAccessor extends NumberValueAccessor {
constructor() {
super(new TestElementRef(new Slider()));
}
}
class TestCheckedValueAccessor extends CheckedValueAccessor {
constructor() {
super(new TestElementRef(new Switch()));
}
}
class TestDateValueAccessor extends DateValueAccessor {
constructor() {
super(new TestElementRef(new DatePicker()));
}
}
class TestSelectedIndexValueAccessor extends SelectedIndexValueAccessor {
constructor() {
super(new TestElementRef(TestSelectedIndexValueAccessor.picker()));
}
static picker(): ListPicker {
const picker = new ListPicker();
picker.items = ['1', '2', '3', '4', '5'];
return picker;
}
}
class TestTimeValueAccessor extends TimeValueAccessor {
constructor() {
super(new TestElementRef(new TimePicker()));
}
}
class TestTextValueAccessor extends TextValueAccessor {
constructor() {
super(new TestElementRef(new TextField()));
}
}
describe('two-way binding via ng-model', () => {
it('converts strings to numbers', () => {
const accessor = new TestNumberValueAccessor();
const defaultValue = accessor.view.value;
accessor.writeValue(null);
expect(accessor.view.value).withContext('setting null should keep the default value').toBe(defaultValue);
accessor.writeValue('42');
expect(accessor.view.value).toBe(42);
accessor.writeValue('blah');
expect(accessor.view.value).withContext('defaults to NaN on parse errors').toBeNaN();
accessor.writeValue(null);
expect(accessor.view.value).withContext('setting null should reset the value').toBe(defaultValue);
});
it('converts strings to bools', () => {
const accessor = new TestCheckedValueAccessor();
const defaultValue = accessor.view.checked;
accessor.writeValue(null);
expect(accessor.view.checked).withContext('setting null should keep the default value').toBe(false);
accessor.writeValue('true');
expect(accessor.view.checked).toBe(true);
accessor.writeValue(null);
expect(accessor.view.checked).withContext('setting null should reset the value').toBe(defaultValue);
expect(() => accessor.writeValue('blah')).not.toThrow();
});
it('converts strings to dates', () => {
const accessor = new TestDateValueAccessor();
const defaultDate = accessor.view.date; // current date time
expect(accessor.view.date).toBeInstanceOf(Date);
let diff = Math.abs(accessor.view.date.getTime() - defaultDate.getTime());
expect(diff < 1000)
.withContext('setting null should reset the value')
.toBeTrue();
accessor.writeValue('2010/03/17');
expect(formatDate(accessor.view.date)).toBe(formatDate(new Date(2010, 2, 17)));
accessor.writeValue(null);
expect(accessor.view.date).toBeInstanceOf(Date);
diff = Math.abs(accessor.view.date.getTime() - defaultDate.getTime());
expect(diff < 1000)
.withContext('setting null should reset the value')
.toBeTrue();
});
it('converts strings to int selection', () => {
const accessor = new TestSelectedIndexValueAccessor();
const defaultValue = accessor.view.selectedIndex;
accessor.writeValue(null);
accessor.ngAfterViewInit();
expect(accessor.view.selectedIndex).withContext('setting null should keep the default value').toBe(defaultValue);
accessor.writeValue('3');
accessor.ngAfterViewInit();
expect(accessor.view.selectedIndex).toBe(3);
accessor.writeValue(null);
expect(accessor.view.selectedIndex).withContext('setting null should reset the value').toBe(defaultValue);
accessor.writeValue('blah');
accessor.ngAfterViewInit();
expect(accessor.view.selectedIndex).withContext('default to NaN on parse errors').toBeNaN();
});
it('converts strings to times', () => {
const accessor = new TestTimeValueAccessor();
expect(() => accessor.writeValue('2010/03/17 12:54')).toThrow();
expect(() => accessor.writeValue('three hours from now')).toThrow();
});
it('converts values to text', () => {
const accessor = new TestTextValueAccessor();
const defaultValue = accessor.view.text;
accessor.writeValue(null);
expect(accessor.view.text).withContext('setting null should keep the default value').toBe(defaultValue);
accessor.writeValue('blah');
expect(accessor.view.text).toBe('blah');
accessor.writeValue(null);
expect(accessor.view.text).withContext('setting null should reset the value').toBe(defaultValue);
accessor.writeValue({ toString: () => 'stringified' });
expect(accessor.view.text).toEqual('stringified');
});
});
function formatDate(date: Date) {
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
}
function formatTime(date: Date) {
return `${date.getHours()}:${date.getMinutes()}`;
}