1 | import { NationalRailWrapper } from '../src/index';
|
2 | import '../src/types/index';
|
3 |
|
4 | describe('Test Wrapper Class', () => {
|
5 | let apiWrapper: NationalRailWrapper;
|
6 |
|
7 | const MAX_TIMEOUT = 10000;
|
8 |
|
9 | beforeAll(() => {
|
10 | const apiToken = process.env.API_TOKEN;
|
11 |
|
12 | if (!apiToken) {
|
13 | fail('No Api Token found');
|
14 | }
|
15 |
|
16 | apiWrapper = new NationalRailWrapper(apiToken);
|
17 | });
|
18 |
|
19 | test(
|
20 | 'should throw an error with an invalid api_key',
|
21 | async () => {
|
22 | const apiToken = 'FAKE_API_KEY';
|
23 | const incorrectApiWrapper = new NationalRailWrapper(apiToken);
|
24 |
|
25 | try {
|
26 | await incorrectApiWrapper.getDepartures({ station: 'LDS' });
|
27 | } catch (err) {
|
28 | expect(err.message).toBe('Cannot parse response');
|
29 | expect(err.response.statusCode).toBe(401);
|
30 | }
|
31 | },
|
32 | MAX_TIMEOUT
|
33 | );
|
34 |
|
35 | test('should return all departures from LDS', async () => {
|
36 | const stationDetails = await apiWrapper.getDepartures({ station: 'LDS' });
|
37 |
|
38 | expect(stationDetails).toHaveProperty('success');
|
39 | expect(stationDetails.success).toBeTruthy();
|
40 |
|
41 | expect(stationDetails).toHaveProperty('data');
|
42 | expect(stationDetails.data).toEqual(
|
43 | expect.arrayContaining([
|
44 | expect.objectContaining({
|
45 | operator: 'TransPennine Express',
|
46 | operatorCode: 'TP',
|
47 | }),
|
48 | expect.objectContaining({
|
49 | operator: 'Northern',
|
50 | operatorCode: 'NT',
|
51 | }),
|
52 | ])
|
53 | );
|
54 | });
|
55 |
|
56 | test('should return all departures from LDS limited only to 3', async () => {
|
57 | const stationDetails = await apiWrapper.getDepartures({ station: 'LDS', count: 1 });
|
58 |
|
59 | expect(stationDetails).toHaveProperty('success');
|
60 | expect(stationDetails.success).toBeTruthy();
|
61 |
|
62 | expect(stationDetails).toHaveProperty('data');
|
63 | expect(stationDetails.data).toHaveLength(1);
|
64 | });
|
65 |
|
66 | test('should return all arrivals from LDS', async () => {
|
67 | const stationDetails = await apiWrapper.getArrivals({ station: 'LDS' });
|
68 |
|
69 | expect(stationDetails).toHaveProperty('success');
|
70 | expect(stationDetails.success).toBeTruthy();
|
71 |
|
72 | expect(stationDetails).toHaveProperty('data');
|
73 | expect(stationDetails.data).toEqual(
|
74 | expect.arrayContaining([
|
75 | expect.objectContaining({
|
76 | operator: 'TransPennine Express',
|
77 | operatorCode: 'TP',
|
78 | }),
|
79 | expect.objectContaining({
|
80 | operator: 'Northern',
|
81 | operatorCode: 'NT',
|
82 | }),
|
83 | ])
|
84 | );
|
85 | });
|
86 |
|
87 | test('should return all arrivals from LDS limited only to 3', async () => {
|
88 | const stationDetails = await apiWrapper.getArrivals({ station: 'LDS', count: 1 });
|
89 |
|
90 | expect(stationDetails).toHaveProperty('success');
|
91 | expect(stationDetails.success).toBeTruthy();
|
92 |
|
93 | expect(stationDetails).toHaveProperty('data');
|
94 | expect(stationDetails.data).toHaveLength(1);
|
95 | });
|
96 |
|
97 | test('should return all arrivals and departutes from LDS', async () => {
|
98 | const stationDetails = await apiWrapper.getAll({ station: 'LDS' });
|
99 |
|
100 | expect(stationDetails).toHaveProperty('success');
|
101 | expect(stationDetails.success).toBeTruthy();
|
102 |
|
103 | expect(stationDetails).toHaveProperty('data');
|
104 | expect(stationDetails.data).toEqual(
|
105 | expect.arrayContaining([
|
106 | expect.objectContaining({
|
107 | operator: 'TransPennine Express',
|
108 | operatorCode: 'TP',
|
109 | }),
|
110 | expect.objectContaining({
|
111 | operator: 'Northern',
|
112 | operatorCode: 'NT',
|
113 | }),
|
114 | ])
|
115 | );
|
116 | });
|
117 |
|
118 | test('should return all arrivals and departutes from LDS limited only to 3', async () => {
|
119 | const stationDetails = await apiWrapper.getAll({ station: 'LDS', count: 2 });
|
120 |
|
121 | expect(stationDetails).toHaveProperty('success');
|
122 | expect(stationDetails.success).toBeTruthy();
|
123 |
|
124 | expect(stationDetails).toHaveProperty('data');
|
125 | expect(stationDetails.data).toHaveLength(2);
|
126 | });
|
127 |
|
128 | test('should return service details for certain serviceId', async () => {
|
129 | const stationDetails = await apiWrapper.getAll({ station: 'LDS', count: 1 });
|
130 | const service = stationDetails.data && stationDetails.data[0];
|
131 | const serviceId = service.serviceID;
|
132 |
|
133 | expect(service).toHaveProperty('serviceID');
|
134 | expect(service).toHaveProperty('operator');
|
135 | expect(service).toHaveProperty('operatorCode');
|
136 |
|
137 | const serviceDetails = await apiWrapper.getServiceDetails({ serviceId });
|
138 | const details = serviceDetails.data && serviceDetails.data;
|
139 |
|
140 | expect(service.rsid).toBe(details.rsid);
|
141 | expect(service.operator).toBe(details.operator);
|
142 | expect(service.operatorCode).toBe(details.operatorCode);
|
143 | });
|
144 | });
|