|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +const test = require('tape'); |
| 6 | +const supertest = require('supertest'); |
| 7 | +const rhoaster = require('rhoaster'); |
| 8 | + |
| 9 | +const testEnvironment = rhoaster({ |
| 10 | + deploymentName: 'nodejs-rest-http-crud', |
| 11 | + nodeVersion: '8.x' |
| 12 | +}); |
| 13 | + |
| 14 | +testEnvironment.deploy() |
| 15 | + .then(runTests) |
| 16 | + .then(_ => test.onFinish(testEnvironment.undeploy)) |
| 17 | + .catch(console.error); |
| 18 | + |
| 19 | +function runTests (route) { |
| 20 | + // GET fruits |
| 21 | + test('/api/fruits', t => { |
| 22 | + t.plan(2); |
| 23 | + supertest(route) |
| 24 | + .get('/api/fruits') |
| 25 | + .expect(200) |
| 26 | + .expect('Content-Type', /json/) |
| 27 | + .then(response => { |
| 28 | + t.equal(Array.isArray(response.body), true, 'response is an Array'); |
| 29 | + t.equal(response.body.length, 3, 'should be initialized with 3 items'); |
| 30 | + t.end(); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + // GET 1 fruit |
| 35 | + test('/api/fruit/:id', t => { |
| 36 | + t.plan(4); |
| 37 | + supertest(route) |
| 38 | + .get('/api/fruits/1') |
| 39 | + .expect(200) |
| 40 | + .expect('Content-Type', /json/) |
| 41 | + .then(response => { |
| 42 | + t.equal(Array.isArray(response.body), false, 'not an array returned'); |
| 43 | + t.equal(response.body.id, 1, 'id is 1'); |
| 44 | + t.equal(response.body.name, 'Apple', 'name is Apple'); |
| 45 | + t.equal(response.body.stock, '10', 'stock is 10'); |
| 46 | + t.end(); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + // GET 1 fruit that doesn't exist |
| 51 | + test('/api/fruit/:id - does not exist', t => { |
| 52 | + t.plan(1); |
| 53 | + supertest(route) |
| 54 | + .get('/api/fruits/10') |
| 55 | + .expect(404) |
| 56 | + .expect('Content-Type', /text/) |
| 57 | + .then(response => { |
| 58 | + t.equal(response.text, 'Item 10 not found', 'return not found string'); |
| 59 | + t.end(); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + // POST a fruit |
| 64 | + test('POST /api/fruit/', t => { |
| 65 | + t.plan(4); |
| 66 | + const fruitData = { |
| 67 | + name: 'Banana', |
| 68 | + stock: '10' |
| 69 | + }; |
| 70 | + |
| 71 | + supertest(route) |
| 72 | + .post('/api/fruits') |
| 73 | + .send(fruitData) |
| 74 | + .expect(201) |
| 75 | + .expect('Content-Type', /json/) |
| 76 | + .then(response => { |
| 77 | + t.equal(Array.isArray(response.body), false, 'not an array returned'); |
| 78 | + t.ok(response.body.id, 'has a new id field'); |
| 79 | + t.equal(response.body.name, fruitData.name, `name is ${fruitData.name}`); |
| 80 | + t.equal(response.body.stock, fruitData.stock, `stock is ${fruitData.stock}`); |
| 81 | + |
| 82 | + // clean up |
| 83 | + supertest(route).delete(`/api/fruits/${response.body.id}`).then(_ => ({})); |
| 84 | + t.end(); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + // PUT a fruit |
| 89 | + test('PUT /api/fruit/:id', t => { |
| 90 | + // t.plan(0); |
| 91 | + const fruitData = { |
| 92 | + name: 'put fruit', |
| 93 | + stock: '10' |
| 94 | + }; |
| 95 | + |
| 96 | + // First create the new fruit |
| 97 | + supertest(route) |
| 98 | + .post('/api/fruits') |
| 99 | + .send(fruitData) |
| 100 | + .expect(201) |
| 101 | + .then(response => { |
| 102 | + const id = response.body.id; |
| 103 | + |
| 104 | + const updatedFruit = { |
| 105 | + name: response.body.name, |
| 106 | + stock: '20' |
| 107 | + }; |
| 108 | + |
| 109 | + supertest(route) |
| 110 | + .put(`/api/fruits/${id}`) |
| 111 | + .send(updatedFruit) |
| 112 | + .expect(204) |
| 113 | + .then(putResponse => { |
| 114 | + // clean up |
| 115 | + t.pass('should return with an empty response'); |
| 116 | + supertest(route).delete(`/api/fruits/${response.body.id}`).then(_ => ({})); |
| 117 | + t.end(); |
| 118 | + }); |
| 119 | + }); |
| 120 | + }); |
| 121 | +} |
0 commit comments