|
1 | 1 | import { PluginOptions } from 'gatsby';
|
2 |
| -import { addLocalePrefix, parsePathPrefix, translatePagePaths, trimRightSlash, trimSlashes } from './path'; |
| 2 | +import { |
| 3 | + addLocalePrefix, |
| 4 | + generatePageContextByPath, |
| 5 | + handleTrailingSlash, |
| 6 | + parsePathPrefix, |
| 7 | + translatePagePaths, |
| 8 | + trimRightSlash, |
| 9 | + trimSlashes, |
| 10 | +} from './path'; |
| 11 | + |
| 12 | +describe('handleTrailingSlash', () => { |
| 13 | + it('should leave trailing slahes, when the option is set to always', () => { |
| 14 | + const expectation = handleTrailingSlash('https://example.com/', 'always'); |
| 15 | + expect(expectation).toBe('https://example.com/'); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should add trailing slahes, when the option is set to always', () => { |
| 19 | + const expectation = handleTrailingSlash('https://example.com', 'always'); |
| 20 | + expect(expectation).toBe('https://example.com/'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should remove trailing slahes, when the option is set to never', () => { |
| 24 | + const expectation = handleTrailingSlash('https://example.com/', 'never'); |
| 25 | + expect(expectation).toBe('https://example.com'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should not add trailing slahes, when the option is set to never', () => { |
| 29 | + const expectation = handleTrailingSlash('https://example.com', 'never'); |
| 30 | + expect(expectation).toBe('https://example.com'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should return root paths', () => { |
| 34 | + const expectation = handleTrailingSlash('/', 'never'); |
| 35 | + expect(expectation).toBe('/'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should return empty paths', () => { |
| 39 | + const expectation = handleTrailingSlash('', 'never'); |
| 40 | + expect(expectation).toBe(''); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should return empty paths', () => { |
| 44 | + const expectation = handleTrailingSlash('', 'never'); |
| 45 | + expect(expectation).toBe(''); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should return paths, when the option is set to ignore', () => { |
| 49 | + let expectation = handleTrailingSlash('https://example.com', 'ignore'); |
| 50 | + expect(expectation).toBe('https://example.com'); |
| 51 | + expectation = handleTrailingSlash('https://example.com/', 'ignore'); |
| 52 | + expect(expectation).toBe('https://example.com/'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should default to always', () => { |
| 56 | + const expectation = handleTrailingSlash('https://example.com'); |
| 57 | + expect(expectation).toBe('https://example.com/'); |
| 58 | + }); |
| 59 | +}); |
3 | 60 |
|
4 | 61 | describe('trimRightSlash', () => {
|
5 | 62 | it('should trim the right slash', () => {
|
@@ -103,4 +160,46 @@ describe('translatePagePaths', () => {
|
103 | 160 | { locale: 'zh-CN', path: '/zh/imprint' },
|
104 | 161 | ]);
|
105 | 162 | });
|
| 163 | + |
| 164 | + describe('generatePageContextByPath', () => { |
| 165 | + const path = '/imprint'; |
| 166 | + const options: PluginOptions = { |
| 167 | + defaultLocale: `en-US`, |
| 168 | + siteUrl: '', |
| 169 | + locales: [ |
| 170 | + { |
| 171 | + locale: `en-US`, |
| 172 | + prefix: `en`, |
| 173 | + slugs: {}, |
| 174 | + messages: {}, |
| 175 | + }, |
| 176 | + { |
| 177 | + locale: `de-CH`, |
| 178 | + prefix: `de`, |
| 179 | + slugs: { |
| 180 | + '/imprint': '/impressum', |
| 181 | + }, |
| 182 | + messages: {}, |
| 183 | + }, |
| 184 | + { |
| 185 | + locale: `zh-CN`, |
| 186 | + prefix: `zh`, |
| 187 | + slugs: {}, |
| 188 | + messages: {}, |
| 189 | + }, |
| 190 | + ], |
| 191 | + plugins: [], |
| 192 | + }; |
| 193 | + |
| 194 | + it('should generate the page context by path', () => { |
| 195 | + const expectation = generatePageContextByPath(path, options); |
| 196 | + expect(expectation).toMatchSnapshot(); |
| 197 | + }); |
| 198 | + |
| 199 | + it('should default to empty prefix', () => { |
| 200 | + const customOptions = { ...options, defaultLocale: 'fr-FR' }; |
| 201 | + const expectation = generatePageContextByPath(path, customOptions); |
| 202 | + expect(expectation).toMatchSnapshot(); |
| 203 | + }); |
| 204 | + }); |
106 | 205 | });
|
0 commit comments