-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from MO-Movia/unit_tests
Unit tests
- Loading branch information
Showing
11 changed files
with
737 additions
and
62 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
// setup for jest because jquery is needed by node-mathquill | ||
window.jQuery = require('jquery'); | ||
// needed to mock this due to execute during loading | ||
document.execCommand = | ||
document.execCommand || function execCommandMock() {}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import UICommand from './UICommand'; | ||
import {Transaction} from 'prosemirror-state'; | ||
import {createEditor, doc, p} from 'jest-prosemirror'; | ||
|
||
describe('UICommand', () => { | ||
const editor = createEditor(doc(p('<cursor>'))); | ||
|
||
beforeAll(() => { | ||
// Create a spy on console (console.error in this case) and provide some mocked implementation | ||
// In mocking global objects it's usually better than simple `jest.fn()` | ||
// because you can `unmock` it in clean way doing `mockRestore` | ||
jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
}); | ||
|
||
afterAll(() => { | ||
// Restore mock after all tests are done, so it won't affect other test suites | ||
console.error.mockRestore(); | ||
}); | ||
|
||
describe('isEnabled', () => { | ||
let uiCmd: UICommand; | ||
|
||
beforeEach(() => (uiCmd = new UICommand())); | ||
|
||
describe('when command is enabled', () => { | ||
xit('should return truthy value', () => { | ||
// TODO: replace this comment with steps to enable the command. | ||
// So skipping this for now | ||
|
||
const output = uiCmd.isEnabled(editor.state, editor.view); | ||
|
||
expect(output).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe('when command is not enabled', () => { | ||
xit('should return falsy value', () => { | ||
// TODO: replace this comment with steps to disable the command. | ||
// So skipping this for now | ||
|
||
const output = uiCmd.isEnabled(editor.state, editor.view); | ||
|
||
expect(output).toBeFalsy(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should respond to UI event', () => { | ||
const uiCmd = new UICommand(); | ||
const respond = uiCmd.shouldRespondToUIEvent({ | ||
type: UICommand.EventType.CLICK, | ||
}); | ||
expect(respond).toEqual(true); | ||
}); | ||
|
||
it('should execute custom', () => { | ||
const uiCmd = new UICommand(); | ||
const tr = uiCmd.executeCustom(editor.state, editor.state.tr, 0, 0); | ||
expect(tr.doc).toBe(editor.state.tr.doc); | ||
}); | ||
|
||
it('should not be active by default', () => { | ||
const uiCmd = new UICommand(); | ||
const active = uiCmd.isActive(editor.state); | ||
expect(active).toEqual(false); | ||
}); | ||
|
||
it('should label be null by default', () => { | ||
const uiCmd = new UICommand(); | ||
const label = uiCmd.renderLabel(editor.state); | ||
expect(label).toEqual(null); | ||
}); | ||
|
||
it('should be disabled if error thrown', () => { | ||
const uiCmd = new UICommand(); | ||
const mockWFUI = jest.fn(); | ||
mockWFUI.mockReturnValue(Promise.reject('this is error')); | ||
uiCmd.waitForUserInput = mockWFUI; | ||
const enabled = uiCmd.isEnabled(editor.state, editor.view); | ||
expect(enabled).toEqual(false); | ||
}); | ||
|
||
xit('should set transaction meta data dryrun to true', () => { | ||
const uiCmd = new UICommand(); | ||
const obj = uiCmd.dryRunEditorStateProxyGetter(editor.state, 'tr'); | ||
expect(obj).toBeInstanceOf(Transaction); | ||
|
||
// TODO: Generally tests should not branch. | ||
// During execution line 86 will abort the test if it does not pass. | ||
// Therefore you will never get to line 92 if it's not a Transaction | ||
|
||
if (obj instanceof Transaction) { | ||
const meta = obj.getMeta('dryrun'); | ||
expect(meta).toEqual(true); | ||
} | ||
}); | ||
|
||
describe('dryRunEditorStateProxyGetter', () => { | ||
let tr: Transaction; | ||
let state; | ||
let uiCmd: UICommand; | ||
|
||
beforeEach(() => { | ||
tr = new Transaction({}); | ||
tr.setMeta = jest.fn().mockImplementation(() => tr); | ||
// use same spy for both cases | ||
state = {tr, other: tr}; | ||
uiCmd = new UICommand(); | ||
}); | ||
|
||
describe('when getting the transaction', () => { | ||
it('should update transaction metadata', () => { | ||
const output = uiCmd.dryRunEditorStateProxyGetter(state, 'tr'); | ||
|
||
expect(tr.setMeta).toHaveBeenCalled(); | ||
expect(output).toBe(state.tr); | ||
}); | ||
}); | ||
|
||
describe('when getting other data', () => { | ||
it('should not update transaction metadata', () => { | ||
const output = uiCmd.dryRunEditorStateProxyGetter(state, 'other'); | ||
|
||
expect(tr.setMeta).not.toHaveBeenCalled(); | ||
expect(output).toBe(state.other); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('dryRunEditorStateProxySetter', () => { | ||
it('should set state property', () => { | ||
const uiCmd = new UICommand(); | ||
const testVal = 'xVal'; | ||
uiCmd.dryRunEditorStateProxySetter(editor.state, 'xTest', testVal); | ||
expect(editor.state.xTest).toEqual(testVal); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.