Skip to content

Commit 5d0b8d2

Browse files
chrisbobbegnprice
authored andcommitted
internalLinks tests [nfc]: Refactor further, with mkCheck
1 parent a22dfaa commit 5d0b8d2

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

src/utils/__tests__/internalLinks-test.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
} from '../internalLinks';
1717
import * as eg from '../../__tests__/lib/exampleData';
1818
import { isUrlRelative } from '../url';
19+
import type { LinkType } from '../internalLinks';
1920

2021
const realm = new URL('https://example.com');
2122
const urlOnRealm = relativeUrl => {
@@ -121,19 +122,19 @@ describe('isNarrowLink', () => {
121122
});
122123

123124
describe('getLinkType', () => {
125+
const mkCheck = (expected: LinkType) => hash => {
126+
expect(getLinkType(new URL(hash, realm), realm)).toBe(expected);
127+
};
128+
124129
test('link containing "stream" is a stream link', () => {
125-
const check = hash => {
126-
expect(getLinkType(new URL(hash, realm), realm)).toBe('stream');
127-
};
130+
const check = mkCheck('stream');
128131
['/#narrow/stream/jest', '/#narrow/stream/stream/', '/#narrow/stream/topic/'].forEach(hash =>
129132
check(hash),
130133
);
131134
});
132135

133136
test('link containing "topic" is a topic link', () => {
134-
const check = hash => {
135-
expect(getLinkType(new URL(hash, realm), realm)).toBe('topic');
136-
};
137+
const check = mkCheck('topic');
137138
[
138139
'/#narrow/stream/jest/topic/test',
139140
'/#narrow/stream/mobile/subject/topic/near/378333',
@@ -145,9 +146,7 @@ describe('getLinkType', () => {
145146
});
146147

147148
test('link containing "pm-with" is a PM link', () => {
148-
const check = hash => {
149-
expect(getLinkType(new URL(hash, realm), realm)).toBe('pm');
150-
};
149+
const check = mkCheck('pm');
151150
[
152151
'/#narrow/pm-with/1,2-group',
153152
'/#narrow/pm-with/1,2-group/near/1',
@@ -156,18 +155,14 @@ describe('getLinkType', () => {
156155
});
157156

158157
test('link containing "is" with valid operand is a special link', () => {
159-
const check = hash => {
160-
expect(getLinkType(new URL(hash, realm), realm)).toBe('special');
161-
};
158+
const check = mkCheck('special');
162159
['/#narrow/is/private', '/#narrow/is/starred', '/#narrow/is/mentioned'].forEach(hash =>
163160
check(hash),
164161
);
165162
});
166163

167164
test('unexpected link shape gives "home"', () => {
168-
const check = hash => {
169-
expect(getLinkType(new URL(hash, realm), realm)).toBe('home');
170-
};
165+
const check = mkCheck('home');
171166
[
172167
// `near` with no operand
173168
'/#narrow/stream/stream/topic/topic/near/',

src/utils/internalLinks.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ export const isNarrowLink = (url: URL, realm: URL): boolean =>
4848
&& url.search === ''
4949
&& /^#narrow\//i.test(url.hash);
5050

51-
type LinkType = 'home' | 'pm' | 'topic' | 'stream' | 'special';
51+
/* PRIVATE: Exported only for tests. */
52+
export type LinkType = 'home' | 'pm' | 'topic' | 'stream' | 'special';
5253

5354
/**
5455
* PRIVATE -- exported only for tests.

0 commit comments

Comments
 (0)