Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle relative route template (#1276)
* Add tests around extractTemplates 404 handling * Resolves relative route.template against ROOT This ensure that given a relative `route.template`, extractTemplates behavior remains intact while also supporting absolute `route.template`. See #1253 (comment) for details * Update documentation for route.template
- Loading branch information
1 parent
581c002
commit a491d84
Showing
3 changed files
with
67 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/react-static/src/static/__tests__/extractTemplates.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import extractTemplates from '../extractTemplates' | ||
|
||
const config = { | ||
paths: { | ||
ROOT: process.cwd(), | ||
}, | ||
} | ||
|
||
test('a 404 route is required when the build is not incremental', async () => { | ||
expect.assertions(1) | ||
try { | ||
await extractTemplates({ | ||
config, | ||
routes: [], | ||
incremental: false, | ||
}) | ||
} catch (error) { | ||
expect(error.message).toBeTruthy() | ||
} | ||
}) | ||
|
||
test('a 404 route is not required when the build is incremental', async () => { | ||
expect.assertions(1) | ||
try { | ||
const state = await extractTemplates({ | ||
config, | ||
routes: [], | ||
incremental: true, | ||
}) | ||
expect(state).toBeTruthy() | ||
} catch (_error) { | ||
throw new Error() | ||
} | ||
}) | ||
|
||
test('the 404 template is the first one', async () => { | ||
const state = await extractTemplates({ | ||
config, | ||
routes: [ | ||
{ path: '/', template: './src/templates/Homepage' }, | ||
{ path: '404', template: './src/templates/404' }, | ||
], | ||
}) | ||
|
||
expect(state.templates[0]).toContain('src/templates/404') | ||
}) | ||
|
||
test('relative routes path resolves against the ROOT', async () => { | ||
const { templates } = await extractTemplates({ | ||
config, | ||
routes: [{ path: '404', template: './src/templates/NotFound' }], | ||
}) | ||
|
||
expect(templates[0]).toBe(`${config.paths.ROOT}/src/templates/NotFound`) | ||
}) | ||
|
||
test('absolute routes path are kept intact', async () => { | ||
const { templates } = await extractTemplates({ | ||
config, | ||
routes: [{ path: '404', template: '/home/src/templates/NotFound' }], | ||
}) | ||
|
||
expect(templates[0]).toBe('/home/src/templates/NotFound') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters