Skip to content

Commit 458fee4

Browse files
fix tests.
1 parent b8c14a1 commit 458fee4

File tree

3 files changed

+118
-102
lines changed

3 files changed

+118
-102
lines changed

src/tests/injectHooks.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('Test that it is possible to inject hooks for any action', () => {
1313
}
1414

1515
beforeAll(async () => {
16-
server = await createServer({}, globalPreCallback)
16+
server = await createServer({}, globalPreCallback, true)
1717
})
1818

1919
afterAll(() => closeServer(server))

src/tests/setupTestServer.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,23 @@ const { json } = require('body-parser')
99
const { migrateDatabase, seedDatabase } = require('./testDatabase.js')
1010
const models = require('./models/index.js')
1111

12-
const createServer = async (options = {}, globalPreCallback = () => null) => {
12+
const createServer = async (
13+
options = {},
14+
globalPreCallback = () => null,
15+
withGeneratedHooks = false
16+
) => {
1317
const app = express()
1418
options = {
1519
spdy: { plain: true },
1620
...options,
1721
}
1822
const httpServer = http.createServer(options, app)
1923

20-
const { server } = setupServer(globalPreCallback, httpServer)
24+
const { server } = setupServer(
25+
globalPreCallback,
26+
httpServer,
27+
withGeneratedHooks
28+
)
2129
await server.start()
2230
app.use(
2331
'/graphql',

src/tests/testSchema.js

Lines changed: 107 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const models = require('./models')
3131
// From https://github.com/mickhansen/graphql-sequelize#options
3232
resolver.contextToOptions = { [EXPECTED_OPTIONS_KEY]: EXPECTED_OPTIONS_KEY }
3333

34-
const graphqlSchemaDeclaration = {}
34+
let graphqlSchemaDeclaration = {}
3535
const types = generateModelTypes(models)
3636
const pubSubInstance = new PubSub()
3737

@@ -452,7 +452,11 @@ graphqlSchemaDeclaration.userLocation = {
452452
},
453453
}
454454

455-
module.exports = (globalPreCallback, httpServer) => {
455+
module.exports = (
456+
globalPreCallback,
457+
httpServer,
458+
withGeneratedHooks = false
459+
) => {
456460
// Creating the WebSocket server
457461
const wsServer = new WebSocketServer({
458462
// This is the `httpServer` we created in a previous step.
@@ -464,108 +468,112 @@ module.exports = (globalPreCallback, httpServer) => {
464468

465469
const protectedModels = ['user', 'company']
466470

467-
return {
468-
server: generateApolloServer({
469-
graphqlSchemaDeclaration: injectHooks({
470-
graphqlSchemaDeclaration,
471-
injectFunctions: {
472-
listBefore: (models, hooks) => {
473-
// Ensure the last hook enforce the rights
474-
hooks.push(({ findOptions }) => {
475-
if (protectedModels.includes(models.name)) {
476-
findOptions.where = {
477-
[Op.and]: [findOptions.where, { id: 1 }],
478-
}
479-
}
480-
return findOptions
481-
})
482-
return hooks
483-
},
484-
listAfter: (models, hooks) => {
485-
hooks.push(({ result }) => {
486-
if (Array.isArray(result)) {
487-
result.forEach((item) => {
488-
if (item.name) {
489-
item.name = `[LIST] ${item.name}`
490-
}
491-
})
492-
}
493-
return result
494-
})
495-
return hooks
496-
},
497-
createBefore: (models, hooks) => {
498-
hooks.push(({ source, args, context, info }) => {
499-
if (args.user && args.user.name) {
500-
args.user.name = `[CREATE] ${args.user.name}`
471+
if (withGeneratedHooks) {
472+
graphqlSchemaDeclaration = injectHooks({
473+
graphqlSchemaDeclaration,
474+
injectFunctions: {
475+
listBefore: (models, hooks) => {
476+
// Ensure the last hook enforce the rights
477+
hooks.push(({ findOptions }) => {
478+
if (protectedModels.includes(models.name)) {
479+
findOptions.where = {
480+
[Op.and]: [findOptions.where, { id: 1 }],
501481
}
502-
return args.user
503-
})
504-
return hooks
505-
},
506-
createAfter: (models, hooks) => {
507-
hooks.push(
508-
({
509-
createdEntity,
510-
source,
511-
args,
512-
context,
513-
info,
514-
setWebhookData,
515-
}) => {
516-
if (createdEntity.name) {
517-
createdEntity.name = `${createdEntity.name} [CREATED]`
482+
}
483+
return findOptions
484+
})
485+
return hooks
486+
},
487+
listAfter: (models, hooks) => {
488+
hooks.push(({ result }) => {
489+
if (Array.isArray(result)) {
490+
result.forEach((item) => {
491+
if (item.name) {
492+
item.name = `[LIST] ${item.name}`
518493
}
519-
return createdEntity
520-
}
521-
)
522-
return hooks
523-
},
524-
updateBefore: (models, hooks) => {
525-
hooks.push(({ source, args, context, info }) => {
526-
if (args.user && args.user.name) {
527-
args.user.name = `[UPDATE] ${args.user.name}`
494+
})
495+
}
496+
return result
497+
})
498+
return hooks
499+
},
500+
createBefore: (models, hooks) => {
501+
hooks.push(({ source, args, context, info }) => {
502+
if (args.user && args.user.name) {
503+
args.user.name = `[CREATE] ${args.user.name}`
504+
}
505+
return args.user
506+
})
507+
return hooks
508+
},
509+
createAfter: (models, hooks) => {
510+
hooks.push(
511+
({
512+
createdEntity,
513+
source,
514+
args,
515+
context,
516+
info,
517+
setWebhookData,
518+
}) => {
519+
if (createdEntity.name) {
520+
createdEntity.name = `${createdEntity.name} [CREATED]`
528521
}
529-
return args.user
530-
})
531-
return hooks
532-
},
533-
updateAfter: (models, hooks) => {
534-
hooks.push(
535-
({
536-
updatedEntity,
537-
previousPropertiesSnapshot,
538-
source,
539-
args,
540-
context,
541-
info,
542-
}) => {
543-
if (updatedEntity.name) {
544-
updatedEntity.name = `${updatedEntity.name} [UPDATED]`
545-
}
546-
return updatedEntity
522+
return createdEntity
523+
}
524+
)
525+
return hooks
526+
},
527+
updateBefore: (models, hooks) => {
528+
hooks.push(({ source, args, context, info }) => {
529+
if (args.user && args.user.name) {
530+
args.user.name = `[UPDATE] ${args.user.name}`
531+
}
532+
return args.user
533+
})
534+
return hooks
535+
},
536+
updateAfter: (models, hooks) => {
537+
hooks.push(
538+
({
539+
updatedEntity,
540+
previousPropertiesSnapshot,
541+
source,
542+
args,
543+
context,
544+
info,
545+
}) => {
546+
if (updatedEntity.name) {
547+
updatedEntity.name = `${updatedEntity.name} [UPDATED]`
547548
}
548-
)
549-
return hooks
550-
},
551-
deleteBefore: (models, hooks) => {
552-
hooks.push(({ where, source, args, context, info }) => {
553-
// Add a timestamp to the where clause
554-
console.log(`[DELETE] Will delete Entity ${where.id}.`)
555-
return where
556-
})
557-
return hooks
558-
},
559-
deleteAfter: (models, hooks) => {
560-
hooks.push(({ deletedEntity, source, args, context, info }) => {
561-
// Log the deletion
562-
console.log(`[DELETE] Entity ${deletedEntity.id} was deleted`)
563-
return deletedEntity
564-
})
565-
return hooks
566-
},
549+
return updatedEntity
550+
}
551+
)
552+
return hooks
553+
},
554+
deleteBefore: (models, hooks) => {
555+
hooks.push(({ where, source, args, context, info }) => {
556+
// Add a timestamp to the where clause
557+
console.log(`[DELETE] Will delete Entity ${where.id}.`)
558+
return where
559+
})
560+
return hooks
567561
},
568-
}),
562+
deleteAfter: (models, hooks) => {
563+
hooks.push(({ deletedEntity, source, args, context, info }) => {
564+
// Log the deletion
565+
console.log(`[DELETE] Entity ${deletedEntity.id} was deleted`)
566+
return deletedEntity
567+
})
568+
return hooks
569+
},
570+
},
571+
})
572+
}
573+
574+
return {
575+
server: generateApolloServer({
576+
graphqlSchemaDeclaration,
569577
customMutations,
570578
types,
571579
models,

0 commit comments

Comments
 (0)