Skip to content

Commit a22dfaa

Browse files
chrisbobbegnprice
authored andcommitted
internalLinks tests [nfc]: Refactor getLinkType tests with checks
1 parent 508b485 commit a22dfaa

File tree

1 file changed

+47
-38
lines changed

1 file changed

+47
-38
lines changed

src/utils/__tests__/internalLinks-test.js

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
/* @flow strict-local */
22
import invariant from 'invariant';
33

4+
// Tell ESLint to recognize `expectStream` as a helper function that
5+
// runs assertions.
6+
/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "check", "expectStream"] }] */
7+
48
import type { Stream } from '../../types';
59
import { streamNarrow, topicNarrow, pmNarrowFromUsersUnsafe, STARRED_NARROW } from '../narrow';
610
import {
@@ -118,55 +122,63 @@ describe('isNarrowLink', () => {
118122

119123
describe('getLinkType', () => {
120124
test('link containing "stream" is a stream link', () => {
121-
expect(getLinkType(new URL('/#narrow/stream/jest', realm), realm)).toBe('stream');
122-
expect(getLinkType(new URL('/#narrow/stream/stream/', realm), realm)).toBe('stream');
123-
expect(getLinkType(new URL('/#narrow/stream/topic/', realm), realm)).toBe('stream');
125+
const check = hash => {
126+
expect(getLinkType(new URL(hash, realm), realm)).toBe('stream');
127+
};
128+
['/#narrow/stream/jest', '/#narrow/stream/stream/', '/#narrow/stream/topic/'].forEach(hash =>
129+
check(hash),
130+
);
124131
});
125132

126133
test('link containing "topic" is a topic link', () => {
127-
expect(getLinkType(new URL('/#narrow/stream/jest/topic/test', realm), realm)).toBe('topic');
128-
expect(
129-
getLinkType(new URL('/#narrow/stream/mobile/subject/topic/near/378333', realm), realm),
130-
).toBe('topic');
131-
expect(getLinkType(new URL('/#narrow/stream/mobile/topic/topic/', realm), realm)).toBe('topic');
132-
expect(getLinkType(new URL('/#narrow/stream/stream/topic/topic/near/1', realm), realm)).toBe(
133-
'topic',
134-
);
135-
expect(getLinkType(new URL('/#narrow/stream/stream/subject/topic/near/1', realm), realm)).toBe(
136-
'topic',
137-
);
138-
139-
expect(getLinkType(new URL('/#narrow/stream/stream/subject/topic', realm), realm)).toBe(
140-
'topic',
141-
);
134+
const check = hash => {
135+
expect(getLinkType(new URL(hash, realm), realm)).toBe('topic');
136+
};
137+
[
138+
'/#narrow/stream/jest/topic/test',
139+
'/#narrow/stream/mobile/subject/topic/near/378333',
140+
'/#narrow/stream/mobile/topic/topic/',
141+
'/#narrow/stream/stream/topic/topic/near/1',
142+
'/#narrow/stream/stream/subject/topic/near/1',
143+
'/#narrow/stream/stream/subject/topic',
144+
].forEach(hash => check(hash));
142145
});
143146

144147
test('link containing "pm-with" is a PM link', () => {
145-
expect(getLinkType(new URL('/#narrow/pm-with/1,2-group', realm), realm)).toBe('pm');
146-
expect(getLinkType(new URL('/#narrow/pm-with/1,2-group/near/1', realm), realm)).toBe('pm');
147-
expect(
148-
getLinkType(new URL('/#narrow/pm-with/a.40b.2Ecom.2Ec.2Ed.2Ecom/near/3', realm), realm),
149-
).toBe('pm');
148+
const check = hash => {
149+
expect(getLinkType(new URL(hash, realm), realm)).toBe('pm');
150+
};
151+
[
152+
'/#narrow/pm-with/1,2-group',
153+
'/#narrow/pm-with/1,2-group/near/1',
154+
'/#narrow/pm-with/a.40b.2Ecom.2Ec.2Ed.2Ecom/near/3',
155+
].forEach(hash => check(hash));
150156
});
151157

152158
test('link containing "is" with valid operand is a special link', () => {
153-
expect(getLinkType(new URL('/#narrow/is/private', realm), realm)).toBe('special');
154-
expect(getLinkType(new URL('/#narrow/is/starred', realm), realm)).toBe('special');
155-
expect(getLinkType(new URL('/#narrow/is/mentioned', realm), realm)).toBe('special');
159+
const check = hash => {
160+
expect(getLinkType(new URL(hash, realm), realm)).toBe('special');
161+
};
162+
['/#narrow/is/private', '/#narrow/is/starred', '/#narrow/is/mentioned'].forEach(hash =>
163+
check(hash),
164+
);
156165
});
157166

158167
test('unexpected link shape gives "home"', () => {
159-
// `near` with no operand
160-
expect(getLinkType(new URL('/#narrow/stream/stream/topic/topic/near/', realm), realm)).toBe(
161-
'home',
162-
);
168+
const check = hash => {
169+
expect(getLinkType(new URL(hash, realm), realm)).toBe('home');
170+
};
171+
[
172+
// `near` with no operand
173+
'/#narrow/stream/stream/topic/topic/near/',
163174

164-
// `is` with invalid operand
165-
expect(getLinkType(new URL('/#narrow/is/men', realm), realm)).toBe('home');
166-
expect(getLinkType(new URL('/#narrow/is/men/stream', realm), realm)).toBe('home');
175+
// `is` with invalid operand
176+
'/#narrow/is/men',
177+
'/#narrow/is/men/stream',
167178

168-
// invalid operand `are`; `stream` operator with no operand
169-
expect(getLinkType(new URL('/#narrow/are/men/stream', realm), realm)).toBe('home');
179+
// invalid operand `are`; `stream` operator with no operand
180+
'/#narrow/are/men/stream',
181+
].forEach(hash => check(hash));
170182
});
171183
});
172184

@@ -209,9 +221,6 @@ describe('getNarrowFromLink', () => {
209221
});
210222

211223
describe('on stream links', () => {
212-
// Tell ESLint to recognize `expectStream` as a helper function that
213-
// runs assertions.
214-
/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "expectStream"] }] */
215224
const expectStream = (operand, streams, expectedStream: null | Stream) => {
216225
expect(get(`#narrow/stream/${operand}`, streams)).toEqual(
217226
expectedStream === null ? null : streamNarrow(expectedStream.stream_id),

0 commit comments

Comments
 (0)