File size: 2,084 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import fromApi from '../from-api';

describe( 'fromApi()', () => {
	test( 'should validate and transform the data successfully.', () => {
		const validResponse = {
			available_times: [ 1483264800, 1483266600, 1483268400 ],
			appointment_timespan: 999,
			next_appointment: { begin_timestamp: 1, end_timestamp: 2, schedule_id: 3 },
			is_blocked: false,
			schedule_id: 123,
		};

		const expectedResult = {
			availableTimes: [ 1483264800000, 1483266600000, 1483268400000 ],
			appointmentTimespan: 999,
			nextAppointment: { beginTimestamp: 1000, endTimestamp: 2000, scheduleId: 3 },
			scheduleId: 123,
			isUserBlocked: false,
		};

		expect( fromApi( validResponse ) ).toEqual( expectedResult );
	} );

	test( 'should leave a null next_appointment as null.', () => {
		const validResponse = {
			available_times: [ 1483264800, 1483266600, 1483268400 ],
			next_appointment: null,
		};

		const expectedResult = {
			availableTimes: [ 1483264800000, 1483266600000, 1483268400000 ],
			nextAppointment: null,
		};

		expect( fromApi( validResponse ) ).toEqual( expectedResult );
	} );

	test( 'should invalidate unexpected field types.', () => {
		const invalidateCall = () => {
			const invalidFieldTypes = [ 'this', 'is', false, 'just wrong.' ];

			fromApi( invalidFieldTypes );
		};

		expect( invalidateCall ).toThrow( Error, 'Failed to validate with JSON schema' );
	} );

	test( 'should invalidate missing begin_timestamp.', () => {
		const invalidateMissingBeginTimestamp = () => {
			const invalidResponse = [
				{
					end_timestamp: 400,
				},
			];

			fromApi( invalidResponse );
		};

		expect( invalidateMissingBeginTimestamp ).toThrow(
			Error,
			'Failed to validate with JSON schema'
		);
	} );

	test( 'should invalidate missing end_timestamp.', () => {
		const invalidateMissingEndTimestamp = () => {
			const invalidResponse = {
				available_times: [
					{
						begin_timestamp: 333,
					},
				],
			};

			fromApi( invalidResponse );
		};

		expect( invalidateMissingEndTimestamp ).toThrow( Error, 'Failed to validate with JSON schema' );
	} );
} );