107 lines
2.8 KiB
Handlebars
107 lines
2.8 KiB
Handlebars
import { test } from '@japa/runner'
|
|
import db from '@adonisjs/lucid/services/db'
|
|
import { loginAsAdmin } from '../../utils/auth.js'
|
|
|
|
test.group('{{pascalCaseName}}Controller', (group) => {
|
|
group.each.setup(async () => {
|
|
await db.beginGlobalTransaction()
|
|
return () => db.rollbackGlobalTransaction()
|
|
})
|
|
|
|
test('index: lists paginated {{snakeCaseName}}', async ({ client, assert }) => {
|
|
const token = await loginAsAdmin(client)
|
|
const res = await client.get('/{{snakeCaseName}}').header('Authorization', `Bearer ${token}`)
|
|
res.assertStatus(200)
|
|
assert.exists(res.body().meta, 'pagination meta exists')
|
|
})
|
|
|
|
test('store: creates a new {{snakeCaseName}}', async ({ client, assert }) => {
|
|
const token = await loginAsAdmin(client)
|
|
|
|
const payload = {
|
|
{{#each createFields}}
|
|
{{column_name}}: {{{sampleValue}}}{{#unless @last}},{{/unless}}
|
|
{{/each}}
|
|
}
|
|
|
|
const res = await client
|
|
.post('/{{snakeCaseName}}')
|
|
.header('Authorization', `Bearer ${token}`)
|
|
.json(payload)
|
|
|
|
res.assertStatus(201)
|
|
const body = res.body()
|
|
{{#each createFields}}
|
|
assert.property(body, '{{column_name}}')
|
|
{{/each}}
|
|
})
|
|
|
|
test('show: returns one {{snakeCaseName}} by id', async ({ client }) => {
|
|
const token = await loginAsAdmin(client)
|
|
|
|
const created = await db
|
|
.table('{{tableName}}')
|
|
.insert(
|
|
{{{insertJson}}}
|
|
)
|
|
.returning('id')
|
|
|
|
const id = Array.isArray(created) ? (created[0]?.id ?? created[0]) : created
|
|
|
|
const res = await client
|
|
.get(`/{{snakeCaseName}}/${id}`)
|
|
.header('Authorization', `Bearer ${token}`)
|
|
|
|
res.assertStatus(200)
|
|
})
|
|
|
|
test('update: updates {{snakeCaseName}}', async ({ client, assert }) => {
|
|
const token = await loginAsAdmin(client)
|
|
|
|
const created = await db
|
|
.table('{{tableName}}')
|
|
.insert(
|
|
{{{insertJson}}}
|
|
)
|
|
.returning('id')
|
|
|
|
const id = Array.isArray(created) ? (created[0]?.id ?? created[0]) : created
|
|
|
|
const patch = {
|
|
{{#each updateFields}}
|
|
{{column_name}}: {{{sampleValue}}}{{#unless @last}},{{/unless}}
|
|
{{/each}}
|
|
}
|
|
|
|
const res = await client
|
|
.put(`/{{snakeCaseName}}/${id}`)
|
|
.header('Authorization', `Bearer ${token}`)
|
|
.json(patch)
|
|
|
|
res.assertStatus(200)
|
|
const body = res.body()
|
|
{{#each updateFields}}
|
|
assert.property(body, '{{column_name}}')
|
|
{{/each}}
|
|
})
|
|
|
|
test('destroy: deletes {{snakeCaseName}}', async ({ client }) => {
|
|
const token = await loginAsAdmin(client)
|
|
|
|
const created = await db
|
|
.table('{{tableName}}')
|
|
.insert(
|
|
{{{insertJson}}}
|
|
)
|
|
.returning('id')
|
|
|
|
const id = Array.isArray(created) ? (created[0]?.id ?? created[0]) : created
|
|
|
|
const res = await client
|
|
.delete(`/{{snakeCaseName}}/${id}`)
|
|
.header('Authorization', `Bearer ${token}`)
|
|
|
|
res.assertStatus(200)
|
|
})
|
|
})
|