Skip to content

Commit

Permalink
fix: handle case when there are no validation errors in flash messages
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 23, 2023
1 parent d47a0e7 commit 489909b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/edge_plugin_adonisjs_session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const edgePluginAdonisJSSession: PluginFn<undefined> = (edge) => {
* Write an if statement
*/
buffer.writeStatement(
`if (!!state.flashMessages.get('errors')[${key}]) {`,
`if (!!state.flashMessages.get('errors', {})[${key}]) {`,
token.filename,
token.loc.start.line
)
Expand All @@ -101,7 +101,7 @@ export const edgePluginAdonisJSSession: PluginFn<undefined> = (edge) => {
* Define a local variable
*/
buffer.writeExpression(
`let messages = state.flashMessages.get('errors')[${key}]`,
`let messages = state.flashMessages.get('errors', {})[${key}]`,
token.filename,
token.loc.start.line
)
Expand Down
47 changes: 46 additions & 1 deletion tests/session.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,17 @@ test.group('Session | Regenerate', () => {
})
})

test.group('Session | Flash', () => {
test.group('Session | Flash', (group) => {
group.setup(async () => {
const router = new RouterFactory().create()
await app.init()

app.container.singleton('router', () => router)

await new EdgeServiceProvider(app).boot()
await new SessionProvider(app).boot()
})

test('flash data using the session store', async ({ assert }) => {
let sessionId: string | undefined

Expand Down Expand Up @@ -1084,6 +1094,41 @@ test.group('Session | Flash', () => {
)
})

test('use error tag when there are no error message', async ({ assert }) => {
edge.registerTemplate('flash_no_errors_messages', {
template: `
@error('username')
@each(message in messages)
<p> {{ message }} </p>
@end
@else
<p> No error message </p>
@end
`,
})

const server = httpServer.create(async (req, res) => {
const request = new RequestFactory().merge({ req, res, encryption }).create()
const response = new ResponseFactory().merge({ req, res, encryption }).create()
const ctx = new HttpContextFactory().merge({ request, response }).create()

const driver = new CookieDriver(sessionConfig.cookie, ctx)
const session = new Session(sessionConfig, driver, emitter, ctx)
await session.initiate(false)

response.send(await ctx.view.render('flash_no_errors_messages'))
await session.commit()
response.finish()
})

const { text } = await supertest(server).get('/prg')

assert.deepEqual(
text.split('\n').map((line) => line.trim()),
['<p> No error message </p>', '']
)
})

test('access flash error messages using the @error tag', async ({ assert }) => {
let sessionId: string | undefined

Expand Down

0 comments on commit 489909b

Please sign in to comment.