|
| 1 | +const request = require('request-promise') |
| 2 | +const { listen, mockDb } = require('./utils') |
| 3 | + |
| 4 | +jest.mock('../src/db', () => mockDb) |
| 5 | +const service = require('../src') |
| 6 | +let url |
| 7 | + |
| 8 | +beforeEach(async () => { |
| 9 | + url = await listen(service) |
| 10 | +}) |
| 11 | + |
| 12 | +afterEach(async () => { |
| 13 | + mockDb._reset() |
| 14 | +}) |
| 15 | + |
| 16 | +describe('single', () => { |
| 17 | + it('should set the views of a non-existant path to one', async () => { |
| 18 | + const body = JSON.parse(await request(`${url}/nonexistant`)) |
| 19 | + expect(body.views).toEqual(1) |
| 20 | + }) |
| 21 | + |
| 22 | + it('should increment the views of an existant path', async () => { |
| 23 | + await request(`${url}/existant`) |
| 24 | + const body = JSON.parse(await request(`${url}/existant`)) |
| 25 | + expect(body.views).toEqual(2) |
| 26 | + }) |
| 27 | + |
| 28 | + it('should return 0 views on a non-existant path if inc is set to false', async () => { |
| 29 | + const body = JSON.parse(await request(`${url}/path?inc=false`)) |
| 30 | + expect(body.views).toEqual(0) |
| 31 | + }) |
| 32 | + |
| 33 | + it('should not increment the views of an existant path if inc is set to false', async () => { |
| 34 | + await request(`${url}/existant`) |
| 35 | + const body = JSON.parse(await request(`${url}/existant?inc=false`)) |
| 36 | + expect(body.views).toEqual(1) |
| 37 | + }) |
| 38 | + |
| 39 | + it('should not return anything for a POST request', async () => { |
| 40 | + const body = await request.post(`${url}/existant`) |
| 41 | + expect(body).toEqual('') |
| 42 | + }) |
| 43 | +}) |
| 44 | + |
| 45 | +describe('all', () => { |
| 46 | + it('should return an empty array if no previous views exist', async () => { |
| 47 | + const body = JSON.parse(await request(`${url}/?all=true`)) |
| 48 | + expect(body.data).toEqual({}) |
| 49 | + expect(body.time).toBeDefined() |
| 50 | + }) |
| 51 | + |
| 52 | + it('should return previous views of one route', async () => { |
| 53 | + await request(`${url}/route`) |
| 54 | + await request(`${url}/route`) |
| 55 | + const body = JSON.parse(await request(`${url}/?all=true`)) |
| 56 | + expect(Object.keys(body.data).length).toBe(1) |
| 57 | + expect(body.data['/route'].views).toBeDefined() |
| 58 | + expect(body.data['/route'].views.length).toBe(2) |
| 59 | + }) |
| 60 | + |
| 61 | + it('should return previous views of all routes', async () => { |
| 62 | + await request(`${url}/route`) |
| 63 | + await request(`${url}/route`) |
| 64 | + await request(`${url}/route2`) |
| 65 | + await request(`${url}/route2`) |
| 66 | + await request(`${url}/route2`) |
| 67 | + const body = JSON.parse(await request(`${url}/?all=true`)) |
| 68 | + expect(Object.keys(body.data).length).toBe(2) |
| 69 | + expect(body.data['/route'].views).toBeDefined() |
| 70 | + expect(body.data['/route'].views.length).toBe(2) |
| 71 | + expect(body.data['/route2'].views).toBeDefined() |
| 72 | + expect(body.data['/route2'].views.length).toBe(3) |
| 73 | + }) |
| 74 | +}) |
0 commit comments