| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- 'use strict'
-
- const test = require('ava')
- const { stub } = require('sinon')
- const Hapi = require('@hapi/hapi')
-
- const plugin = require('../lib/plugins/profile')
-
- const Profile = require('../lib/models/profile')
- const Tag = require('../lib/models/tag')
-
- /**
- * Route parameters
- */
- const params = {
- profile_id: 1,
- target_id: 2,
- reinsert: true,
- include_profile: false,
- }
- const mockReturn = {
- profile: {
- profile_id: 99,
- user: {},
- responses: [],
- tags: [],
- },
- tags: [],
- }
- const pathToTest = {
- method: 'GET',
- url: `/${params.profile_id}`,
- }
-
- test('path /<profile_id> should return ok', async t => {
- /**
- * Create a new server and register services,
- * models and routes for testing
- * -
- * NOTE: We use a mocked registerModel() and register
- * models manually. Normally this is handled by
- * Schwifty at runtime.
- */
- const server = Hapi.server()
- /**
- * Overload so we don't register any models
- * using the plugin call (see plugins/profile.js)
- * and Manually load the model we need for the test
- */
- server.registerModel = () => {}
- server.models = () => ({ Profile, Tag })
- /**
- * Register Routes and Services as usual
- */
- await plugin.register(server)
- /**
- * Replace Objection model methods with our own mock functions
- * !: Janky - might be better to temp knex sqlite instance
- */
- stub(server.models()['Tag'], 'query').returns(mockReturn.tags)
- stub(server.models()['Profile'], 'query').returns({
- // Mocked for getProfile()
- where: () => ({
- first: () => ({
- withGraphFetched: () => ({
- withGraphFetched: () => ({
- withGraphFetched: () => mockReturn.profile,
- }),
- }),
- }),
- }),
- })
-
- /**
- * Test the server with registered models and services
- */
- const { payload } = await server.inject(pathToTest)
- const res = JSON.parse(payload)
-
- t.deepEqual(res.ok, true)
- t.deepEqual(res.data.profile_id, mockReturn.profile.profile_id)
- t.notDeepEqual(res.data, mockReturn.profile)
-
- t.true(Object.keys(res.data).includes('profile_description'))
- t.true(Object.keys(res.data).includes('profile_languages'))
- })
|