From bf60ea5e6d119147a8a38935f2623f5ff495fe42 Mon Sep 17 00:00:00 2001 From: Eddie Antonio Santos Date: Thu, 10 Jan 2019 14:58:00 -0700 Subject: [PATCH] Refactor: move global test helpers to unit_tests/helpers.js. --- .../headless/worker-initialization.js | 19 +----- .../headless/worker-predict-dummy.js | 37 ---------- .../unit_tests/headless/worker-predict.js | 43 ------------ common/predictive-text/unit_tests/helpers.js | 67 +++++++++++++++++++ common/predictive-text/unit_tests/test.sh | 4 +- 5 files changed, 70 insertions(+), 100 deletions(-) create mode 100644 common/predictive-text/unit_tests/helpers.js diff --git a/common/predictive-text/unit_tests/headless/worker-initialization.js b/common/predictive-text/unit_tests/headless/worker-initialization.js index 8b23fdef20..c5fe049f71 100644 --- a/common/predictive-text/unit_tests/headless/worker-initialization.js +++ b/common/predictive-text/unit_tests/headless/worker-initialization.js @@ -114,33 +114,16 @@ describe('LMLayerWorker', function() { }); }); - /** - * Creates a MessageEvent (for inter-worker communication), with the given data payload. - * @param {*} data - */ - function createMessageEventWithData(data) { - return { data }; - } - /** * Deprecation warning: Soon, models will not be required to be passed as source code; * when this happens, tests should refrain from sending source code for the model * parameter. */ + // TODO: Use dummyModel defined in unit_tests/helpers.js function dummyModel() { return 'return {model: {}, configuration: {}}'; } - /** - * Returns reasonable defaults for the default capabilities - * to initialize the LMLayer. - */ - function defaultCapabilities() { - return { - maxLeftContextCodeUnits: 64 - }; - } - /** * Returns the last message received in a pretty string format. * @param {sinon.SinonFake} fakePostMessage diff --git a/common/predictive-text/unit_tests/headless/worker-predict-dummy.js b/common/predictive-text/unit_tests/headless/worker-predict-dummy.js index e81727ada2..0c4ed00f71 100644 --- a/common/predictive-text/unit_tests/headless/worker-predict-dummy.js +++ b/common/predictive-text/unit_tests/headless/worker-predict-dummy.js @@ -206,41 +206,4 @@ describe('LMLayerWorker dummy model', function() { futureSuggestions[3]); }); }); - - /** - * Capabilities of a keyboard that will ONLY send left-sided capabilities. - * The keyboard does not support deleting to the right. - * - * @returns Capabilities - */ - function defaultCapabilities() { - return { - maxLeftContextCodeUnits: 64, - }; - } - - /** - * Returns a transform that does nothing. - * - * @returns Transform - */ - function zeroTransform() { - return { - insert: '', - deleteLeft: 0, - }; - } - - /** - * Returns a context of an empty buffer. - * - * @returns Context - */ - function emptyContext() { - return { - left: '', - startOfBuffer: true, - endOfBuffer: true - }; - } }); diff --git a/common/predictive-text/unit_tests/headless/worker-predict.js b/common/predictive-text/unit_tests/headless/worker-predict.js index 1df5ef2bba..cd21fc8edb 100644 --- a/common/predictive-text/unit_tests/headless/worker-predict.js +++ b/common/predictive-text/unit_tests/headless/worker-predict.js @@ -48,47 +48,4 @@ describe('LMLayerWorker', function () { sinon.restore(); }); }); - - // Helpers (TODO: factor out!) - - /** - * Creates a MessageEvent (for inter-worker communication), with the given data payload. - * @param {*} data - */ - function createMessageEventWithData(data) { - return { data }; - } - - /** - * A valid model that suggests exactly what you want it to suggest. - */ - function dummyModel(futureSuggestions) { - return { - type: 'dummy', - futureSuggestions: futureSuggestions || [] - }; - } - /** - * Returns reasonable defaults for the default capabilities - * to initialize the LMLayer. - */ - function defaultCapabilities() { - return { - maxLeftContextCodeUnits: 64 - }; - } - - /** - * Context of an empty buffer; no text, at both the start and end of the buffer. - */ - function emptyContext() { - return { left: '', startOfBuffer: true, endOfBuffer: true }; - } - - /** - * This transform, when applied, makes no changes to the buffer. - */ - function zeroTransform() { - return { insert: '', deleteLeft: 0 }; - } }); diff --git a/common/predictive-text/unit_tests/helpers.js b/common/predictive-text/unit_tests/helpers.js new file mode 100644 index 0000000000..fd291a88c8 --- /dev/null +++ b/common/predictive-text/unit_tests/helpers.js @@ -0,0 +1,67 @@ +/** + * @file helpers + * + * Globally-defined helper functions for use in in Mocha tests. + */ + +// Choose the appropriate global object. Either `global` in +// Node, or `window` in browsers. +var _ = global || window; + +/** + * Creates a MessageEvent (for inter-worker communication), with the given data payload. + * + * @param {*} data + */ +_.createMessageEventWithData = function createMessageEventWithData(data) { + return { data }; +} + +/** + * A valid model that suggests exactly what you want it to suggest. + * + * @returns {ModelDescription} + */ +_.dummyModel = function dummyModel(futureSuggestions) { + return { + type: 'dummy', + futureSuggestions: futureSuggestions || [] + }; +} +/** + * Capabilities of a keyboard that will ONLY send left-sided capabilities. + * The keyboard does not support deleting to the right. + * + * @returns {Capabilities} + */ +_.defaultCapabilities = function defaultCapabilities() { + return { + maxLeftContextCodeUnits: 64 + }; +} + +/** + * Returns the Context of an empty buffer; no text, at both the start and + * end of the buffer. + * + * @returns {Context} + */ +_.emptyContext = function emptyContext() { + return { + left: '', + startOfBuffer: true, + endOfBuffer: true + }; +} + +/** + * Returns a Transform that, when applied, makes no changes to the buffer. + * + * @returns {Transform} + */ +_.zeroTransform = function zeroTransform() { + return { + insert: '', + deleteLeft: 0, + }; +} diff --git a/common/predictive-text/unit_tests/test.sh b/common/predictive-text/unit_tests/test.sh index fd8862586d..bae66faee5 100755 --- a/common/predictive-text/unit_tests/test.sh +++ b/common/predictive-text/unit_tests/test.sh @@ -45,7 +45,7 @@ test-browsers ( ) { # Defaults get_builder_OS # return: os_id="linux"|"mac"|"win" -FLAGS= +FLAGS="--require ./unit_tests/helpers" CI_REPORTING=0 RUN_HEADLESS=1 RUN_BROWSERS=1 @@ -80,4 +80,4 @@ fi # Run browser-based tests. if (( RUN_BROWSERS )); then test-browsers || fail "Browser-based tests failed!" -fi \ No newline at end of file +fi