|
1 | 1 | const Hapi = require('hapi'); |
2 | 2 | const tap = require('tap'); |
3 | 3 | const plugin = require('../index'); |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
4 | 6 |
|
5 | 7 | tap.test('can configure a route to return csv instead of json', async(t) => { |
6 | 8 | const server = await new Hapi.Server({ port: 8080 }); |
7 | 9 | server.route({ |
8 | 10 | method: 'get', |
9 | 11 | path: '/normal', |
10 | 12 | handler(request, h) { |
11 | | - return { |
12 | | - fields: ['car', 'price', 'color'], |
13 | | - data: [ |
14 | | - { |
15 | | - car: 'Audi', |
16 | | - price: 40000, |
17 | | - color: 'blue' |
18 | | - }, { |
19 | | - car: 'BMW', |
20 | | - price: 35000, |
21 | | - color: 'black' |
22 | | - }, { |
23 | | - car: 'Porsche', |
24 | | - price: 60000, |
25 | | - color: 'green' |
26 | | - } |
27 | | - ] |
28 | | - }; |
| 13 | + return [ |
| 14 | + { |
| 15 | + car: 'Audi', |
| 16 | + price: 40000, |
| 17 | + color: 'blue' |
| 18 | + }, { |
| 19 | + car: 'BMW', |
| 20 | + price: 35000, |
| 21 | + color: 'black' |
| 22 | + }, { |
| 23 | + car: 'Porsche', |
| 24 | + price: 60000, |
| 25 | + color: 'green' |
| 26 | + } |
| 27 | + ]; |
29 | 28 | } |
30 | 29 | }); |
31 | 30 |
|
@@ -64,10 +63,7 @@ tap.test('can configure a route to return csv instead of json', async(t) => { |
64 | 63 | }); |
65 | 64 | t.equal(csvResponse.statusCode, 200, 'returns HTTP OK'); |
66 | 65 | t.equal(typeof csvResponse.result, 'string', 'returns a string value'); |
67 | | - const rows = csvResponse.result.split('\n'); |
68 | | - t.notEqual(rows[0].indexOf('car'), -1, 'top row of csv is the headers'); |
69 | | - t.notEqual(rows[0].indexOf('price'), -1, 'top row of csv is the headers'); |
70 | | - t.notEqual(rows[0].indexOf('color'), -1, 'top row of csv is the headers'); |
| 66 | + t.equal(csvResponse.result, fs.readFileSync(path.join(__dirname, 'output1.txt'), 'utf-8'), 'returns correct output'); |
71 | 67 | const jsonResponse = await server.inject({ |
72 | 68 | method: 'get', |
73 | 69 | url: '/normal' |
@@ -129,8 +125,7 @@ tap.test('will pass config options to json2csv', async(t) => { |
129 | 125 | url: '/path1.csv' |
130 | 126 | }); |
131 | 127 | t.equal(csvResponse.statusCode, 200, 'returns HTTP OK'); |
132 | | - t.equal(csvResponse.result.indexOf('$car$'), 0, 'applied the json2csv route options'); |
133 | | - t.equal(csvResponse.result.indexOf('$car$_$price$_$color$'), 0, 'applied the json2csv plugin options'); |
| 128 | + t.equal(csvResponse.result, fs.readFileSync(path.join(__dirname, 'output2.txt'), 'utf-8'), 'returns correct output'); |
134 | 129 | await server.stop(); |
135 | 130 | t.end(); |
136 | 131 | }); |
0 commit comments