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

Commit

Permalink
Fix tests to be independent on the implementation of invariant
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Matthew McKeen authored and facebook-github-bot committed Jun 21, 2018
1 parent 0f58b64 commit 81cc54b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
33 changes: 23 additions & 10 deletions src/model/modifier/__tests__/AtomicBlockUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,26 @@ const EditorState = require('EditorState');
const SelectionState = require('SelectionState');

const getSampleStateForTesting = require('getSampleStateForTesting');
const invariant = require('invariant');

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

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

const getInvariantViolation = msg => {
try {
/* eslint-disable fb-www/sprintf-like-args */
invariant(false, msg);
/* eslint-enable fb-www/sprintf-like-args */
} catch (e) {
return e;
}

throw new Error('We should never reach here!');
};

const toggleExperimentalTreeDataSupport = enabled => {
jest.doMock('gkx', () => name => {
return name === 'draft_tree_data_support' ? enabled : false;
Expand Down Expand Up @@ -251,7 +264,7 @@ test("mustn't move atomic next to itself with collapsed selection", () => {
focusOffset: beforeAtomicBlock.getLength(),
}),
);
}).toThrow(new Error('Block cannot be moved next to itself.'));
}).toThrow(getInvariantViolation('Block cannot be moved next to itself.'));

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

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

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

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

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

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

// Move atomic block below itself
expect(() => {
Expand All @@ -341,7 +354,7 @@ test("mustn't move atomic next to itself with collapsed selection", () => {
}),
'after',
);
}).toThrow(new Error('Block cannot be moved next to itself.'));
}).toThrow(getInvariantViolation('Block cannot be moved next to itself.'));
});

/**
Expand Down Expand Up @@ -558,7 +571,7 @@ test("mustn't move atomic next to itself", () => {
focusOffset: beforeAtomicBlock.getLength(),
}),
);
}).toThrow(new Error('Block cannot be moved next to itself.'));
}).toThrow(getInvariantViolation('Block cannot be moved next to itself.'));

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

test('must be able to insert atomic block when experimentalTreeDataSupport is enabled', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const SelectionState = require('SelectionState');

const getSampleStateForTesting = require('getSampleStateForTesting');
const insertFragmentIntoContentState = require('insertFragmentIntoContentState');
const invariant = require('invariant');

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

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

const getInvariantViolation = msg => {
try {
/* eslint-disable fb-www/sprintf-like-args */
invariant(false, msg);
/* eslint-enable fb-www/sprintf-like-args */
} catch (e) {
return e;
}

throw new Error('We should never reach here!');
};

const createFragment = (fragment = {}, experimentalTreeDataSupport = false) => {
const ContentBlockNodeRecord = experimentalTreeDataSupport
? ContentBlockNode
Expand Down Expand Up @@ -385,7 +398,7 @@ test('must throw an error when trying to apply ContentBlockNode fragments when s
]),
),
).toThrow(
new Error(
getInvariantViolation(
'`insertFragment` should not be called when a container node is selected.',
),
);
Expand Down

0 comments on commit 81cc54b

Please sign in to comment.