|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { getSiteRewrite, getSiteRewriteData, normalizeSiteRewrite, SITE_PREFIX } from './utils'; |
| 3 | + |
| 4 | +describe('utils', () => { |
| 5 | + describe('getSiteRewrite', () => { |
| 6 | + const data = { |
| 7 | + siteName: 'jss', |
| 8 | + }; |
| 9 | + |
| 10 | + it('should return a string', () => { |
| 11 | + expect(getSiteRewrite('/pathname', data)).to.be.a('string'); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should return the path with the site name when pathname starts with "/"', () => { |
| 15 | + const pathname = '/some/path'; |
| 16 | + const result = getSiteRewrite(pathname, data); |
| 17 | + expect(result).to.equal(`/${SITE_PREFIX}${data.siteName}/some/path`); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should return the path with the site name when pathname not starts with "/"', () => { |
| 21 | + const pathname = 'some/path'; |
| 22 | + const result = getSiteRewrite(pathname, data); |
| 23 | + expect(result).to.equal(`/${SITE_PREFIX}${data.siteName}/some/path`); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should return the root path with the site name', () => { |
| 27 | + const pathname = '/'; |
| 28 | + const result = getSiteRewrite(pathname, data); |
| 29 | + expect(result).to.equal(`/${SITE_PREFIX}${data.siteName}/`); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('getSiteRewriteData', () => { |
| 34 | + const defaultSiteName = 'foo'; |
| 35 | + |
| 36 | + it('should return a MultiSiteRewriteData object', () => { |
| 37 | + expect(getSiteRewriteData('/some/path', defaultSiteName)).to.be.an('object'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should return the multisite data from the rewrite path', () => { |
| 41 | + const pathname = `/some/path/${SITE_PREFIX}bar/`; |
| 42 | + const result = getSiteRewriteData(pathname, defaultSiteName); |
| 43 | + expect(result.siteName).to.equal('bar'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should return the default site name when pathname does not contain site name', () => { |
| 47 | + const pathname = '/some/path'; |
| 48 | + const result = getSiteRewriteData(pathname, defaultSiteName); |
| 49 | + expect(result.siteName).to.equal(defaultSiteName); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should return empty site name when pathname is missing site name', () => { |
| 53 | + const pathname = `/some/path/${SITE_PREFIX}/`; |
| 54 | + const result = getSiteRewriteData(pathname, defaultSiteName); |
| 55 | + expect(result.siteName).to.equal(defaultSiteName); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('normalizeSiteRewrite', () => { |
| 60 | + it('should return a string', () => { |
| 61 | + expect(normalizeSiteRewrite('/some/path')).to.be.a('string'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should return the pathname when it does not contain site prefix', () => { |
| 65 | + const pathname = '/some/path'; |
| 66 | + const result = normalizeSiteRewrite(pathname); |
| 67 | + expect(result).to.equal(pathname); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should return the pathname without the site name', () => { |
| 71 | + const pathname = `/${SITE_PREFIX}foo/some/path`; |
| 72 | + const result = normalizeSiteRewrite(pathname); |
| 73 | + expect(result).to.equal('/some/path'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should return the root pathname without the site name', () => { |
| 77 | + const pathname = `/${SITE_PREFIX}foo/`; |
| 78 | + const result = normalizeSiteRewrite(pathname); |
| 79 | + expect(result).to.equal('/'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should return the root pathname without the site name when pathname not ends with "/"', () => { |
| 83 | + const pathname = `/${SITE_PREFIX}foo`; |
| 84 | + const result = normalizeSiteRewrite(pathname); |
| 85 | + expect(result).to.equal('/'); |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments