-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
5 changed files
with
164 additions
and
23 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
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,12 +1,12 @@ | ||
import { generatePrefix } from '../components/VersionDropdown/utils'; | ||
import { baseUrl } from './base-url'; | ||
import { assertTrailingSlash } from './assert-trailing-slash'; | ||
import { normalizePath } from './normalize-path'; | ||
import { DOTCOM_BASE_URL } from './base-url'; | ||
import { localizePath } from './locale'; | ||
|
||
export const getUrl = (branchUrlSlug, project, siteMetadata, siteBasePrefix, slug) => { | ||
if (branchUrlSlug === 'legacy') { | ||
return `${baseUrl()}legacy/?site=${project}`; | ||
const legacyPath = localizePath(`/docs/legacy/?site=${project}`); | ||
return DOTCOM_BASE_URL + legacyPath; | ||
} | ||
const prefixWithVersion = generatePrefix(branchUrlSlug, siteMetadata, siteBasePrefix); | ||
return assertTrailingSlash(normalizePath(`${prefixWithVersion}/${slug}`)); | ||
return localizePath(`${prefixWithVersion}/${slug}`); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,67 @@ | ||
import { AVAILABLE_LANGUAGES, getLocaleMapping } from '../../../src/utils/locale'; | ||
|
||
it.each([ | ||
['https://www.mongodb.com/docs/', '/'], | ||
['https://www.mongodb.com/docs', '/about/page/'], | ||
['https://www.mongodb.com', 'introduction'], | ||
])('returns a valid mapping of URLs', (siteUrl, slug) => { | ||
const mapping = getLocaleMapping(siteUrl, slug); | ||
expect(Object.keys(mapping)).toHaveLength(AVAILABLE_LANGUAGES.length); | ||
expect(mapping).toMatchSnapshot(); | ||
import { AVAILABLE_LANGUAGES, getLocaleMapping, localizePath } from '../../../src/utils/locale'; | ||
|
||
describe('getLocaleMapping', () => { | ||
it.each([ | ||
['https://www.mongodb.com/docs/', '/'], | ||
['https://www.mongodb.com/docs', '/about/page/'], | ||
['https://www.mongodb.com', 'introduction'], | ||
])('returns a valid mapping of URLs', (siteUrl, slug) => { | ||
const mapping = getLocaleMapping(siteUrl, slug); | ||
expect(Object.keys(mapping)).toHaveLength(AVAILABLE_LANGUAGES.length); | ||
expect(mapping).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('localizePath', () => { | ||
let windowSpy; | ||
|
||
beforeEach(() => { | ||
windowSpy = jest.spyOn(window, 'window', 'get'); | ||
}); | ||
|
||
afterEach(() => { | ||
windowSpy.mockRestore(); | ||
}); | ||
|
||
it.each([ | ||
['/', '/zh-cn/'], | ||
['/page/slug', '/zh-cn/page/slug/'], | ||
['page/slug', 'zh-cn/page/slug/'], | ||
])('returns localized path when no code is set', (slug, expectedRes) => { | ||
// Pretend page exists on translated site | ||
windowSpy.mockImplementation(() => ({ | ||
location: { | ||
pathname: '/zh-cn/docs-test/drivers/node/current', | ||
}, | ||
})); | ||
const res = localizePath(slug); | ||
expect(res).toEqual(expectedRes); | ||
}); | ||
|
||
it.each([ | ||
['/', 'ko-kr', '/ko-kr/'], | ||
['/page/slug', 'pt-br', '/pt-br/page/slug/'], | ||
['page/slug', 'zh-cn', 'zh-cn/page/slug/'], | ||
])('returns localized path when code is set', (slug, code, expectedRes) => { | ||
const res = localizePath(slug, code); | ||
expect(res).toEqual(expectedRes); | ||
}); | ||
|
||
it.each([ | ||
['/', '/'], | ||
['/page/slug', '/page/slug/'], | ||
['page/slug', 'page/slug/'], | ||
])('returns the same page slug when English is found by default', (slug, expectedRes) => { | ||
const res = localizePath(slug); | ||
expect(res).toEqual(expectedRes); | ||
}); | ||
|
||
it.each([ | ||
['/', '/'], | ||
['/page/slug', '/page/slug/'], | ||
['page/slug', 'page/slug/'], | ||
])('gracefully defaults to English path when invalid language is passed in', (slug, expectedRes) => { | ||
const res = localizePath(slug, 'beep-boop'); | ||
expect(res).toEqual(expectedRes); | ||
}); | ||
}); |