const db = require("./models/index"); const { beforeAll, afterAll } = require('@jest/globals'); const { Auth: Auth, User: User, Player: Player, Role: Role, PitchType: PitchType, Team: Team, Position: Position } = db; const Op = db.Sequelize.Op; beforeAll(async () => { await db.sequelize.sync({ force: true }); // Reset DB once for the entire test suite await Role.destroy({ where: {} }); await Role.bulkCreate([ { name: 'player' }, { name: 'coach' }, { name: 'admin' } ]); const playerRole = await Role.findAll({ where: { name: { [Op.eq]: 'player' } } }); const coachRole = await Role.findAll({ where: { name: { [Op.eq]: 'coach' } } }); await Auth.destroy({ where: {} }); await User.destroy({ where: {} }); await Player.destroy({ where: {} }); await Team.destroy({ where: {} }); await Auth.create({ email: 'player@example.com', password: 'hash1234' }).then(auth => { return User.create({ firstName: 'Alice', lastName: 'Player', dateOfBirth: '1990-01-01', authId: auth.id }); }).then(async user => { await user.setRoles(playerRole); return Player.create({ height: 172.7, weight: 70.3, gender: 'female', bats: 'right', throws: 'right', jerseyNumber: 24, state: 'active', userId: user.id }) }); await Auth.create({ email: 'coach@example.com', password: 'hash1234' }).then(auth => { return User.create({ firstName: 'Bob', lastName: 'Coach', dateOfBirth: '1990-01-01', authId: auth.id }); }).then(user => { return user.setRoles(coachRole); }); await PitchType.destroy({ where: {} }); await PitchType.bulkCreate([ { name: 'Fastball', abbreviation: 'FB' }, { name: 'Curveball', abbreviation: 'CB' }, { name: 'Slider', abbreviation: 'SL' }, { name: 'Changeup', abbreviation: 'CH' }, { name: 'Cutter', abbreviation: 'CUT' }, { name: 'Sweeper', abbreviation: 'SW' }, { name: 'Slurve', abbreviation: 'SLV' } ]); await Position.destroy({ where: {} }); await Position.bulkCreate([ { name: 'Pitcher', abbreviation: 'P', description: '' }, { name: 'Catcher', abbreviation: 'C', description: '' }, { name: 'First Baseman', abbreviation: '1B', description: '' }, { name: 'Second Baseman', abbreviation: '2B', description: '' }, { name: 'Third Baseman', abbreviation: '3B', description: '' }, { name: 'Short Stop', abbreviation: 'SS', description: '' }, { name: 'Left Fielder', abbreviation: 'LF', description: '' }, { name: 'Center Fielder', abbreviation: 'CF', description: '' }, { name: 'Right Fielder', abbreviation: 'RF', description: '' }, ]); }); afterAll(async () => { await db.sequelize.close(); // Close connection after all tests });