Add more integration tests #31
@ -4,6 +4,7 @@ import setup from '../test/integration-setup.js';
|
||||
|
||||
const STATUS_OK = 200;
|
||||
const STATUS_NOT_FOUND = 404;
|
||||
const STATUS_BAD_REQUEST = 400;
|
||||
|
||||
setup(test);
|
||||
|
||||
@ -112,3 +113,65 @@ test('not found /notamodel', async (t) => {
|
||||
const { statusCode } = await server.inject({ url, method });
|
||||
t.is(statusCode, STATUS_NOT_FOUND);
|
||||
});
|
||||
|
||||
test('destroyScope /players/returnsOne', async (t) => {
|
||||
const { server, instances, sequelize: { models: { Player } } } = t.context;
|
||||
const { player1, player2 } = instances;
|
||||
// this doesn't exist in our fixtures
|
||||
const url = '/players/returnsOne';
|
||||
const method = 'DELETE';
|
||||
|
||||
const presentPlayers = await Player.findAll();
|
||||
const playerIds = presentPlayers.map(({ id }) => id);
|
||||
t.truthy(playerIds.includes(player1.id));
|
||||
t.truthy(playerIds.includes(player2.id));
|
||||
|
||||
const { result, statusCode } = await server.inject({ url, method });
|
||||
t.is(statusCode, STATUS_OK);
|
||||
t.is(result.id, player1.id);
|
||||
|
||||
const nonDeletedPlayers = await Player.findAll();
|
||||
t.is(nonDeletedPlayers.length, presentPlayers.length - 1);
|
||||
});
|
||||
|
||||
test('destroyScope /players/returnsNone', async (t) => {
|
||||
const { server, instances, sequelize: { models: { Player } } } = t.context;
|
||||
const { player1, player2 } = instances;
|
||||
// this doesn't exist in our fixtures
|
||||
const url = '/players/returnsNone';
|
||||
const method = 'DELETE';
|
||||
|
||||
const presentPlayers = await Player.findAll();
|
||||
const playerIds = presentPlayers.map(({ id }) => id);
|
||||
t.truthy(playerIds.includes(player1.id));
|
||||
t.truthy(playerIds.includes(player2.id));
|
||||
|
||||
const { statusCode } = await server.inject({ url, method });
|
||||
t.is(statusCode, STATUS_NOT_FOUND);
|
||||
|
||||
const nonDeletedPlayers = await Player.findAll();
|
||||
const nonDeletedPlayerIds = nonDeletedPlayers.map(({ id }) => id);
|
||||
t.truthy(nonDeletedPlayerIds.includes(player1.id));
|
||||
t.truthy(nonDeletedPlayerIds.includes(player2.id));
|
||||
});
|
||||
|
||||
test('destroyScope invalid scope /players/invalid', async (t) => {
|
||||
const { server, instances, sequelize: { models: { Player } } } = t.context;
|
||||
const { player1, player2 } = instances;
|
||||
// this doesn't exist in our fixtures
|
||||
const url = '/players/invalid';
|
||||
const method = 'DELETE';
|
||||
|
||||
const presentPlayers = await Player.findAll();
|
||||
const playerIds = presentPlayers.map(({ id }) => id);
|
||||
t.truthy(playerIds.includes(player1.id));
|
||||
t.truthy(playerIds.includes(player2.id));
|
||||
|
||||
const { statusCode } = await server.inject({ url, method });
|
||||
t.is(statusCode, STATUS_BAD_REQUEST);
|
||||
|
||||
const nonDeletedPlayers = await Player.findAll();
|
||||
const nonDeletedPlayerIds = nonDeletedPlayers.map(({ id }) => id);
|
||||
t.truthy(nonDeletedPlayerIds.includes(player1.id));
|
||||
t.truthy(nonDeletedPlayerIds.includes(player2.id));
|
||||
});
|
||||
|
13
test/fixtures/models/player.js
vendored
13
test/fixtures/models/player.js
vendored
@ -7,6 +7,7 @@ export default (sequelize, DataTypes) => {
|
||||
},
|
||||
name: DataTypes.STRING,
|
||||
teamId: DataTypes.INTEGER,
|
||||
active: DataTypes.BOOLEAN,
|
||||
}, {
|
||||
classMethods: {
|
||||
associate: (models) => {
|
||||
@ -15,5 +16,17 @@ export default (sequelize, DataTypes) => {
|
||||
});
|
||||
},
|
||||
},
|
||||
scopes: {
|
||||
returnsOne: {
|
||||
where: {
|
||||
active: true,
|
||||
},
|
||||
},
|
||||
returnsNone: {
|
||||
where: {
|
||||
name: 'notaname',
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
|
@ -58,7 +58,9 @@ export default (test) => {
|
||||
const { Player, Team, City } = t.context.sequelize.models;
|
||||
const city1 = await City.create({ name: 'Healdsburg' });
|
||||
const team1 = await Team.create({ name: 'Baseballs', cityId: city1.id });
|
||||
const player1 = await Player.create({ name: 'Pinot', teamId: team1.id });
|
||||
const player1 = await Player.create({
|
||||
name: 'Pinot', teamId: team1.id, active: true,
|
||||
});
|
||||
const player2 = await Player.create({ name: 'Syrah', teamId: team1.id });
|
||||
t.context.instances = { city1, team1, player1, player2 };
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user