'use strict' const test = require('ava') const { stub } = require('sinon') const Hapi = require('@hapi/hapi') const plugin = require('../lib/plugins/profile') const MatchQueue = require('../lib/models/matchqueue') /** * Route parameters */ const params = { profile_id: 1, target_id: 2, reinsert: true, include_profile: false, } const mockReturn = { queue: [ { mocked_profile_id: 1, target_id: 2, is_deleted: false }, { mocked_profile_id: 2, target_id: 3, is_deleted: false }, { mocked_profile_id: 3, target_id: 1, is_deleted: false }, ], } const pathToTest = { method: 'PATCH', url: `/${params.profile_id}/queue/${params.target_id}/delete?include_profile=${params.include_profile}&reinsert=${params.reinsert}`, } test('path //queue/ should return ok on PATCH', 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 = () => ({ MatchQueue }) /** * 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()['MatchQueue'], 'query').returns({ // Mocked for markAsDeleted() patch: () => ({ where: () => ({ andWhere: () => ({ first: () => ({ patch: 'bop' }), }), }), }), insert: queueRecord => { t.is(queueRecord.profile_id, params.profile_id) t.is(queueRecord.target_id, params.target_id) t.is(queueRecord.is_deleted, !params.reinsert) }, // Mocked for getQueue() where: () => ({ andWhere: (cmd, val) => mockReturn.queue, }), }) /** * 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, mockReturn.queue.map(entry => entry.target_id), ) })