-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app): redirect by accept-language header (#282)
* feat(app): add/modify tests for locale redirection * feat(app): redirect by accept language
- Loading branch information
Kohei Asai
authored
Mar 28, 2021
1 parent
e32abb7
commit ae2ca9d
Showing
6 changed files
with
121 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,77 @@ | ||
import { Browser, Page, Response, webkit } from "playwright"; | ||
import { Browser, BrowserContext, Page, Response, webkit } from "playwright"; | ||
|
||
describe("Locale Navigation", () => { | ||
let browser: Browser; | ||
let page: Page; | ||
|
||
beforeAll(async () => { | ||
browser = await webkit.launch(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
page = await browser.newPage(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await page.close(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await browser.close(); | ||
}); | ||
|
||
describe("/", () => { | ||
it("redirects with locale search params when it was missing", async () => { | ||
let initialResponse!: Response; | ||
describe.each([ | ||
["en-US", "en-US"], | ||
["en-GB", "en-US"], | ||
["en", "en-US"], | ||
["ja-JP", "ja-JP"], | ||
["ja", "ja-JP"], | ||
["fr-FR", "en-US"], | ||
])("when you prefer %s", (preferredLocale, expectedLocale) => { | ||
let context!: BrowserContext; | ||
let page: Page; | ||
|
||
page.on("response", (response) => { | ||
if (response.request().url() === "http://localhost:3000/") { | ||
initialResponse = response; | ||
} | ||
beforeAll(async () => { | ||
context = await browser.newContext({ locale: preferredLocale }); | ||
page = await context.newPage(); | ||
}); | ||
|
||
await page.goto("http://localhost:3000/"); | ||
beforeEach(async () => { | ||
page = await context.newPage(); | ||
}); | ||
|
||
expect(initialResponse.status()).toBe(308); | ||
expect(page.url()).toBe("http://localhost:3000/?hl=en-US"); | ||
}); | ||
afterEach(async () => { | ||
await page.close(); | ||
}); | ||
|
||
it("doesn't redirect when the valid locale search params is given", async () => { | ||
let initialResponse!: Response; | ||
afterAll(async () => { | ||
await context.close(); | ||
}); | ||
|
||
it("redirects with locale search params when it was missing", async () => { | ||
let initialResponse!: Response; | ||
|
||
page.on("response", (response) => { | ||
if (response.request().url() === "http://localhost:3000/") { | ||
initialResponse = response; | ||
} | ||
}); | ||
|
||
await page.goto("http://localhost:3000/"); | ||
|
||
page.on("response", (response) => { | ||
if (response.request().url() === "http://localhost:3000/?hl=en-US") { | ||
initialResponse = response; | ||
} | ||
expect(initialResponse.status()).toBe(308); | ||
expect(page.url()).toBe(`http://localhost:3000/?hl=${expectedLocale}`); | ||
}); | ||
|
||
await page.goto("http://localhost:3000/?hl=en-US"); | ||
it("doesn't redirect when the valid locale search params is given", async () => { | ||
let initialResponse!: Response; | ||
|
||
expect(initialResponse.status()).toBe(200); | ||
expect(page.url()).toBe("http://localhost:3000/?hl=en-US"); | ||
page.on("response", (response) => { | ||
if ( | ||
response.request().url() === | ||
`http://localhost:3000/?hl=${expectedLocale}` | ||
) { | ||
initialResponse = response; | ||
} | ||
}); | ||
|
||
await page.goto(`http://localhost:3000/?hl=${expectedLocale}`); | ||
|
||
expect(initialResponse.status()).toBe(200); | ||
expect(page.url()).toBe(`http://localhost:3000/?hl=${expectedLocale}`); | ||
}); | ||
}); | ||
}); | ||
}); |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
ae2ca9d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
kohei-dev-storybook – ./
kohei-dev-storybook.vercel.app
kohei-dev-storybook-git-master-axross.vercel.app
kohei-dev-storybook-axross.vercel.app
storybook.kohei.dev