|
1 |
| -import app from './server'; |
2 |
| - |
3 |
| -describe('Application', () => { |
4 |
| - it('should start the server', (done) => { |
5 |
| - app.once('started', () => { |
6 |
| - app.close(); |
7 |
| - done(); |
8 |
| - }); |
9 |
| - app.start(); |
| 1 | +/* eslint-disable global-require */ |
| 2 | +describe('server', () => { |
| 3 | + let loopback, boot; |
| 4 | + const mockApp = {}; |
| 5 | + const getApp = () => require('./server').default; |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + jest.resetModules(); |
| 9 | + jest.mock('loopback'); |
| 10 | + jest.mock('loopback-boot'); |
| 11 | + loopback = require('loopback'); |
| 12 | + boot = require('loopback-boot'); |
| 13 | + loopback.mockReturnValue(mockApp); |
| 14 | + }); |
| 15 | + |
| 16 | + it('instances loopback', () => { |
| 17 | + getApp(); |
| 18 | + expect(loopback).toBeCalled(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('adds start function', () => { |
| 22 | + expect(getApp().start).toBeInstanceOf(Function); |
| 23 | + }); |
| 24 | + |
| 25 | + it('adds close function', () => { |
| 26 | + expect(getApp().close).toBeInstanceOf(Function); |
| 27 | + }); |
| 28 | + |
| 29 | + it('boot application', () => { |
| 30 | + getApp(); |
| 31 | + expect(boot).toBeCalledWith( |
| 32 | + mockApp, |
| 33 | + expect.objectContaining({ |
| 34 | + appRootDir: __dirname, |
| 35 | + scriptExtensions: expect.arrayContaining([ |
| 36 | + '.js', '.json', '.node', '.ejs', |
| 37 | + ]), |
| 38 | + }), |
| 39 | + expect.any(Function), |
| 40 | + ); |
| 41 | + }); |
| 42 | + |
| 43 | + it('throws when application boot fails', () => { |
| 44 | + boot.mockImplementation((a, o, done) => done('foo')); |
| 45 | + expect(getApp).toThrowError('foo'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('not throws when application boot success', () => { |
| 49 | + boot.mockImplementation((a, o, done) => done()); |
| 50 | + expect(getApp).not.toThrow(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('calls express listen when start is called', () => { |
| 54 | + const listen = jest.fn(); |
| 55 | + loopback.mockReturnValue({ listen }); |
| 56 | + getApp().start(); |
| 57 | + expect(listen).toBeCalledWith(expect.any(Function)); |
| 58 | + }); |
| 59 | + |
| 60 | + it('emit started when start is called', () => { |
| 61 | + const get = jest.fn(() => 'foo'); |
| 62 | + const listen = jest.fn(cb => cb()); |
| 63 | + const emit = jest.fn(); |
| 64 | + loopback.mockReturnValue({ listen, get, emit }); |
| 65 | + getApp().start(); |
| 66 | + expect(emit).toBeCalledWith('started'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('log api endpoint when start is called', () => { |
| 70 | + const get = jest.fn(() => 'foo'); |
| 71 | + const listen = jest.fn(cb => cb()); |
| 72 | + const emit = jest.fn(); |
| 73 | + console.log = jest.fn(); |
| 74 | + loopback.mockReturnValue({ listen, get, emit }); |
| 75 | + getApp().start(); |
| 76 | + expect(console.log).toBeCalledWith( |
| 77 | + expect.any(String), |
| 78 | + 'foo/api', |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it('log explorer url when start is called', () => { |
| 83 | + const get = jest.fn(term => (term === 'url' ? 'url' : { mountPath: 'foo' })); |
| 84 | + const listen = jest.fn(cb => cb()); |
| 85 | + const emit = jest.fn(); |
| 86 | + console.log = jest.fn(); |
| 87 | + loopback.mockReturnValue({ listen, get, emit }); |
| 88 | + getApp().start(); |
| 89 | + expect(console.log).toBeCalledWith( |
| 90 | + expect.any(String), |
| 91 | + 'url', |
| 92 | + 'foo', |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + it('close server when close is called', () => { |
| 97 | + const close = jest.fn(); |
| 98 | + const listen = jest.fn(() => ({ close })); |
| 99 | + loopback.mockReturnValue({ listen }); |
| 100 | + getApp().start(); |
| 101 | + getApp().close(); |
| 102 | + expect(close).toBeCalled(); |
10 | 103 | });
|
11 | 104 | });
|
0 commit comments