1import path from 'path';
2
3import linkRewrite from './remark-link-rewrite.js';
4
5// helper functions to construct remark nodes
6const makeFile = filePath => ({
7  cwd: '/absolue/path/to/docs',
8  history: [path.join('/absolue/path/to/docs/pages', filePath)],
9});
10
11const rewrite = (file, url) => {
12  const link = { type: 'link', url };
13  const tree = { type: 'root', children: [link] };
14  linkRewrite()(tree, file);
15  return link.url;
16};
17
18describe('only rewrites internal links', () => {
19  const file = makeFile('index.mdx');
20
21  it('skips https://external.link', () => {
22    expect(rewrite(file, 'https://external.link')).toBe('https://external.link');
23  });
24
25  it('skips http://external.link?some=query', () => {
26    expect(rewrite(file, 'http://external.link?some=query')).toBe(
27      'http://external.link?some=query'
28    );
29  });
30});
31
32describe('maintains header reference', () => {
33  it('resolves hash for index', () => {
34    const file = makeFile('index.mdx');
35    expect(rewrite(file, './#a-header')).toBe('/#a-header');
36  });
37
38  it('resolves hash for nested page sibling', () => {
39    const file = makeFile('push-notifications/overview.mdx');
40    expect(rewrite(file, './using-fcm.mdx#a-header')).toBe(
41      '/push-notifications/using-fcm/#a-header'
42    );
43  });
44
45  it('resolves hash from nested to index', () => {
46    const file = makeFile('push-notifications/overview.mdx');
47    expect(rewrite(file, '../index.mdx#a-header')).toBe('/#a-header');
48  });
49});
50
51describe('from pages/index.mdx', () => {
52  const file = makeFile('index.mdx');
53
54  it('resolves guides to /guides', () => {
55    expect(rewrite(file, 'guides')).toBe('/guides');
56  });
57
58  it('resolves guides.md to /guides/', () => {
59    expect(rewrite(file, 'guides.mdx')).toBe('/guides/');
60  });
61
62  it('resolves ./guides.md to /guides/', () => {
63    expect(rewrite(file, './guides.mdx')).toBe('/guides/');
64  });
65
66  it('resolves ./nested/reference.md to /nested/reference/', () => {
67    expect(rewrite(file, './nested/reference.mdx')).toBe('/nested/reference/');
68  });
69});
70
71describe('from pages/push-notifications/overview.mdx', () => {
72  const file = makeFile('push-notifications/overview.mdx');
73
74  it('resolves using-fcm to /push-notifications/using-fcm', () => {
75    expect(rewrite(file, 'using-fcm')).toBe('/push-notifications/using-fcm');
76  });
77
78  it('resolves using-fcm.md to /push-notifications/using-fcm/', () => {
79    expect(rewrite(file, 'using-fcm.mdx')).toBe('/push-notifications/using-fcm/');
80  });
81
82  it('resolves ./using-fcm.md to /push-notifications/using-fcm/', () => {
83    expect(rewrite(file, './using-fcm.mdx')).toBe('/push-notifications/using-fcm/');
84  });
85
86  it('resolves ../guides.md to /guides/', () => {
87    expect(rewrite(file, '../guides.mdx')).toBe('/guides/');
88  });
89
90  it('resolves ./ to /push-notifications', () => {
91    expect(rewrite(file, './')).toBe('/push-notifications');
92  });
93
94  it('resolves ../ to /', () => {
95    expect(rewrite(file, '../')).toBe('/');
96  });
97
98  it('resolves ../index.md to /', () => {
99    expect(rewrite(file, '../index.mdx')).toBe('/');
100  });
101});
102
103describe('from pages/versions/latest/sdk/app-auth.mdx', () => {
104  const file = makeFile('versions/latest/sdk/app-auth.mdx');
105
106  it('resolves app-loading.md to /versions/latest/sdk/app-loading/', () => {
107    expect(rewrite(file, 'app-loading.mdx')).toBe('/versions/latest/sdk/app-loading/');
108  });
109
110  it('resolves ./app-loading.md to /versions/latest/sdk/app-loading/', () => {
111    expect(rewrite(file, './app-loading.mdx')).toBe('/versions/latest/sdk/app-loading/');
112  });
113
114  it('resolves ../config/app.md#android to /versions/latest/config/app/#android', () => {
115    expect(rewrite(file, '../config/app.mdx#android')).toBe('/versions/latest/config/app/#android');
116  });
117
118  it('resolves ../../../debugging/runtime-issue.md to /debugging/runtime-issue/', () => {
119    expect(rewrite(file, '../../../debugging/runtime-issue.mdx')).toBe('/debugging/runtime-issue/');
120  });
121});
122
123describe('from pages/debugging/runtime-issue.mdx', () => {
124  const file = makeFile('debugging/runtime-issue.mdx');
125
126  it('resolves hash only to same file', () => {
127    expect(rewrite(file, '#some-header')).toBe('/debugging/runtime-issue/#some-header');
128  });
129
130  it('resolves hash with ./ to same file', () => {
131    expect(rewrite(file, './#some-header')).toBe('/debugging/runtime-issue/#some-header');
132  });
133});
134