Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.

Commit 81cc54b

Browse files
Matthew McKeenfacebook-github-bot
authored andcommitted
Fix tests to be independent on the implementation of invariant
Summary: This fixes these tests to be independent of the implementation of invariant so that these tests can work in multiple environments. Differential Revision: D8580484 fbshipit-source-id: d0b07b6dc62b8f04d0610e84d43fe902b67431d7
1 parent 0f58b64 commit 81cc54b

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

src/model/modifier/__tests__/AtomicBlockUtils-test.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,26 @@ const EditorState = require('EditorState');
2424
const SelectionState = require('SelectionState');
2525

2626
const getSampleStateForTesting = require('getSampleStateForTesting');
27+
const invariant = require('invariant');
2728

2829
const {editorState, contentState, selectionState} = getSampleStateForTesting();
2930

3031
const initialBlock = contentState.getBlockMap().first();
3132
const ENTITY_KEY = Entity.create('TOKEN', 'MUTABLE');
3233
const CHARACTER = ' ';
3334

35+
const getInvariantViolation = msg => {
36+
try {
37+
/* eslint-disable fb-www/sprintf-like-args */
38+
invariant(false, msg);
39+
/* eslint-enable fb-www/sprintf-like-args */
40+
} catch (e) {
41+
return e;
42+
}
43+
44+
throw new Error('We should never reach here!');
45+
};
46+
3447
const toggleExperimentalTreeDataSupport = enabled => {
3548
jest.doMock('gkx', () => name => {
3649
return name === 'draft_tree_data_support' ? enabled : false;
@@ -251,7 +264,7 @@ test("mustn't move atomic next to itself with collapsed selection", () => {
251264
focusOffset: beforeAtomicBlock.getLength(),
252265
}),
253266
);
254-
}).toThrow(new Error('Block cannot be moved next to itself.'));
267+
}).toThrow(getInvariantViolation('Block cannot be moved next to itself.'));
255268

256269
// Move atomic block above itself by moving it after preceding block
257270
expect(() => {
@@ -264,7 +277,7 @@ test("mustn't move atomic next to itself with collapsed selection", () => {
264277
}),
265278
'after',
266279
);
267-
}).toThrow(new Error('Block cannot be moved next to itself.'));
280+
}).toThrow(getInvariantViolation('Block cannot be moved next to itself.'));
268281

269282
// Move atomic block above itself by replacement
270283
expect(() => {
@@ -278,7 +291,7 @@ test("mustn't move atomic next to itself with collapsed selection", () => {
278291
focusOffset: atomicBlock.getLength(),
279292
}),
280293
);
281-
}).toThrow(new Error('Block cannot be moved next to itself.'));
294+
}).toThrow(getInvariantViolation('Block cannot be moved next to itself.'));
282295

283296
// Move atomic block above itself
284297
expect(() => {
@@ -290,7 +303,7 @@ test("mustn't move atomic next to itself with collapsed selection", () => {
290303
}),
291304
'before',
292305
);
293-
}).toThrow(new Error('Block cannot be moved next to itself.'));
306+
}).toThrow(getInvariantViolation('Block cannot be moved next to itself.'));
294307

295308
// Move atomic block below itself by moving it before following block by replacement
296309
expect(() => {
@@ -302,7 +315,7 @@ test("mustn't move atomic next to itself with collapsed selection", () => {
302315
focusKey: afterAtomicBlock.getKey(),
303316
}),
304317
);
305-
}).toThrow(new Error('Block cannot be moved next to itself.'));
318+
}).toThrow(getInvariantViolation('Block cannot be moved next to itself.'));
306319

307320
// Move atomic block below itself by moving it before following block
308321
expect(() => {
@@ -315,7 +328,7 @@ test("mustn't move atomic next to itself with collapsed selection", () => {
315328
}),
316329
'before',
317330
);
318-
}).toThrow(new Error('Block cannot be moved next to itself.'));
331+
}).toThrow(getInvariantViolation('Block cannot be moved next to itself.'));
319332

320333
// Move atomic block below itself by replacement
321334
expect(() => {
@@ -329,7 +342,7 @@ test("mustn't move atomic next to itself with collapsed selection", () => {
329342
focusOffset: atomicBlock.getLength(),
330343
}),
331344
);
332-
}).toThrow(new Error('Block cannot be moved next to itself.'));
345+
}).toThrow(getInvariantViolation('Block cannot be moved next to itself.'));
333346

334347
// Move atomic block below itself
335348
expect(() => {
@@ -341,7 +354,7 @@ test("mustn't move atomic next to itself with collapsed selection", () => {
341354
}),
342355
'after',
343356
);
344-
}).toThrow(new Error('Block cannot be moved next to itself.'));
357+
}).toThrow(getInvariantViolation('Block cannot be moved next to itself.'));
345358
});
346359

347360
/**
@@ -558,7 +571,7 @@ test("mustn't move atomic next to itself", () => {
558571
focusOffset: beforeAtomicBlock.getLength(),
559572
}),
560573
);
561-
}).toThrow(new Error('Block cannot be moved next to itself.'));
574+
}).toThrow(getInvariantViolation('Block cannot be moved next to itself.'));
562575

563576
// Move atomic block below itself by moving it before following block by
564577
// replacement
@@ -573,7 +586,7 @@ test("mustn't move atomic next to itself", () => {
573586
focusOffset: 2,
574587
}),
575588
);
576-
}).toThrow(new Error('Block cannot be moved next to itself.'));
589+
}).toThrow(getInvariantViolation('Block cannot be moved next to itself.'));
577590
});
578591

579592
test('must be able to insert atomic block when experimentalTreeDataSupport is enabled', () => {

src/model/transaction/__tests__/insertFragmentIntoContentState-test.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const SelectionState = require('SelectionState');
2424

2525
const getSampleStateForTesting = require('getSampleStateForTesting');
2626
const insertFragmentIntoContentState = require('insertFragmentIntoContentState');
27+
const invariant = require('invariant');
2728

2829
const {contentState, selectionState} = getSampleStateForTesting();
2930
const {List, Map} = Immutable;
@@ -37,6 +38,18 @@ const DEFAULT_BLOCK_CONFIG = {
3738

3839
const initialBlock = contentState.getBlockMap().first();
3940

41+
const getInvariantViolation = msg => {
42+
try {
43+
/* eslint-disable fb-www/sprintf-like-args */
44+
invariant(false, msg);
45+
/* eslint-enable fb-www/sprintf-like-args */
46+
} catch (e) {
47+
return e;
48+
}
49+
50+
throw new Error('We should never reach here!');
51+
};
52+
4053
const createFragment = (fragment = {}, experimentalTreeDataSupport = false) => {
4154
const ContentBlockNodeRecord = experimentalTreeDataSupport
4255
? ContentBlockNode
@@ -385,7 +398,7 @@ test('must throw an error when trying to apply ContentBlockNode fragments when s
385398
]),
386399
),
387400
).toThrow(
388-
new Error(
401+
getInvariantViolation(
389402
'`insertFragment` should not be called when a container node is selected.',
390403
),
391404
);

0 commit comments

Comments
 (0)