|
1 | 1 | /* @flow strict-local */
|
2 | 2 | import invariant from 'invariant';
|
3 | 3 |
|
| 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 | + |
4 | 8 | import type { Stream } from '../../types';
|
5 | 9 | import { streamNarrow, topicNarrow, pmNarrowFromUsersUnsafe, STARRED_NARROW } from '../narrow';
|
6 | 10 | import {
|
@@ -118,55 +122,63 @@ describe('isNarrowLink', () => {
|
118 | 122 |
|
119 | 123 | describe('getLinkType', () => {
|
120 | 124 | 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 | + ); |
124 | 131 | });
|
125 | 132 |
|
126 | 133 | 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)); |
142 | 145 | });
|
143 | 146 |
|
144 | 147 | 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)); |
150 | 156 | });
|
151 | 157 |
|
152 | 158 | 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 | + ); |
156 | 165 | });
|
157 | 166 |
|
158 | 167 | 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/', |
163 | 174 |
|
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', |
167 | 178 |
|
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)); |
170 | 182 | });
|
171 | 183 | });
|
172 | 184 |
|
@@ -209,9 +221,6 @@ describe('getNarrowFromLink', () => {
|
209 | 221 | });
|
210 | 222 |
|
211 | 223 | 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"] }] */ |
215 | 224 | const expectStream = (operand, streams, expectedStream: null | Stream) => {
|
216 | 225 | expect(get(`#narrow/stream/${operand}`, streams)).toEqual(
|
217 | 226 | expectedStream === null ? null : streamNarrow(expectedStream.stream_id),
|
|
0 commit comments