From 8fd4efc62152a46b211f3f5b923b32ed22f8c82f Mon Sep 17 00:00:00 2001 From: Slytherin <31225007+Divy123@users.noreply.github.com> Date: Tue, 22 Jan 2019 20:13:39 +0530 Subject: [PATCH] Add tests for notify() (#686) * Added HTML-UI test * Added test for notify() * Updated Readme's --- CONTRIBUTING.md | 1 + README.md | 9 ++++++++- examples/lib/defaultHtmlStepUi.js | 8 +++++++- package.json | 4 +++- test/ui/user-interface.js | 31 +++++++++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 test/ui/user-interface.js diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a172dc29..046c3494 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -175,6 +175,7 @@ There are four events in all: * `UI.onComplete(options.step)` must be emitted whenever the output of a draw call is ready. An argument, that is the DataURL of the output image must be passed in. * `UI.onRemove(options.step)` is emitted automatically and the module should not emit it. +* `UI.notify(msg,id)` must be emmited when a notification has to be produced. ### Name and description diff --git a/README.md b/README.md index 19debbef..6ee7c67f 100644 --- a/README.md +++ b/README.md @@ -549,6 +549,10 @@ for a module. This can be used, for instance, to update the DIV with the new ima and remove the loading GIF generated above. * `onRemove` : This event is triggered when a module is removed. This can be used, for instance, to remove the DIV generated above. +* `notify` : This event is triggered whenever we need to shoot a notification to the +user-interface.For example when the step is not available, we can shoot a notification, +by sending appropriate message.For HTML UI it adds a DOM node to the browser, for CLI +and node , it logs the notification output to the respective console. How to define these functions: @@ -557,7 +561,8 @@ sequencer.setUI({ onSetup: function(step) {}, onDraw: function(step) {}, onComplete: function(step) {}, - onRemove: function(step) {} + onRemove: function(step) {}, + notify: function(msg,id) {} }); ``` @@ -607,3 +612,5 @@ not specified, the name of a loaded image defaults to a name like "image1", Details of all modules can be sought using `sequencer.modulesInfo()`. This method returns an object which defines the name and inputs of the modules. If a module name (hyphenated) is passed in the method, then only the details of that module are returned. + +The `notify` function takes two parameters `msg` and `id`, former being the message to be displayed on console (in case of CLI and node ) and a HTML component(in browser). The id is optional and is useful for HTML interface to give appropriate IDs. \ No newline at end of file diff --git a/examples/lib/defaultHtmlStepUi.js b/examples/lib/defaultHtmlStepUi.js index 17c677f9..7c7c721e 100644 --- a/examples/lib/defaultHtmlStepUi.js +++ b/examples/lib/defaultHtmlStepUi.js @@ -9,7 +9,7 @@ // See documetation for more details. function DefaultHtmlStepUi(_sequencer, options) { - + options = options || {}; var stepsEl = options.stepsEl || document.querySelector("#steps"); var selectStepSel = options.selectStepSel = options.selectStepSel || "#selectStep"; @@ -295,3 +295,9 @@ function DefaultHtmlStepUi(_sequencer, options) { notify: notify } } + +if(typeof window === "undefined"){ + module.exports={ + DefaultHtmlStepUi: DefaultHtmlStepUi + } +} diff --git a/package.json b/package.json index add13d04..28e35a02 100644 --- a/package.json +++ b/package.json @@ -5,10 +5,11 @@ "main": "src/ImageSequencer.js", "scripts": { "debug": "TEST=true node ./index.js -i ./examples/images/monarch.png -s invert", - "test": "TEST=true tape test/**/*.js test/*.js | tap-spec; browserify test/modules/image-sequencer.js test/modules/chain.js test/modules/meta-modules.js test/modules/replace.js test/modules/import-export.js test/modules/run.js test/modules/dynamic-imports.js | tape-run --render=\"tap-spec\"", + "test": "TEST=true tape test/ui/*.js test/**/*.js test/*.js test/ui/user-interface.js | tap-spec; browserify test/modules/image-sequencer.js test/modules/chain.js test/modules/meta-modules.js test/modules/replace.js test/modules/import-export.js test/modules/run.js test/modules/dynamic-imports.js test/util/parse-input.js | tape-run --render=\"tap-spec\"", "setup": "npm i && npm i -g grunt grunt-cli", "start": "grunt serve" }, + "repository": { "type": "git", "url": "git+https://github.com/publiclab/image-sequencer.git" @@ -35,6 +36,7 @@ "imagejs": "0.0.9", "imgareaselect": "git://github.com/jywarren/imgareaselect.git#v1.0.0-rc.2", "jquery": "^3.3.1", + "jsdom": "^11.12.0", "jsqr": "^1.1.1", "lodash": "^4.17.5", "ndarray-gaussian-filter": "^1.0.0", diff --git a/test/ui/user-interface.js b/test/ui/user-interface.js new file mode 100644 index 00000000..925f7422 --- /dev/null +++ b/test/ui/user-interface.js @@ -0,0 +1,31 @@ +var test = require('tape'); +var jsdom = require('jsdom'); +var JSDOM = jsdom.JSDOM; + +var DOM = new JSDOM(``); + +global.document = DOM.window.document; + +var DefaultHtmlStepUi = require('../../examples/lib/defaultHtmlStepUi').DefaultHtmlStepUi; +var sequencer = require('../../src/ImageSequencer.js')(); +var UserInterface = require('../../src/ui/UserInterface'); + +test('Notify function works for all three UIs', function (t) { + //Mocking console.log for testing default notify() + function doesLogMessage(f, message) { + var oldLog = console.log, + result = false; + console.log = function (s) { + if (s == message) { + result = true; + } + }; + f('Test Message'); + console.log = oldLog; + return result; + } + t.equal(doesLogMessage(UserInterface().notify, 'Test Message'),true,'Default notify() produces correct output'); + sequencer.setUI(DefaultHtmlStepUi(sequencer)); + t.equal(typeof sequencer.events.notify, "function", "Html UI contains notify function"); + t.end(); +});