Skip to content

Commit

Permalink
Add tests for notify() (#686)
Browse files Browse the repository at this point in the history
* Added HTML-UI test

* Added test for notify()

* Updated Readme's
  • Loading branch information
Divy123 authored and jywarren committed Jan 22, 2019
1 parent 413a235 commit 8fd4efc
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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) {}
});
```
Expand Down Expand Up @@ -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.
8 changes: 7 additions & 1 deletion examples/lib/defaultHtmlStepUi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -295,3 +295,9 @@ function DefaultHtmlStepUi(_sequencer, options) {
notify: notify
}
}

if(typeof window === "undefined"){
module.exports={
DefaultHtmlStepUi: DefaultHtmlStepUi
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand Down
31 changes: 31 additions & 0 deletions test/ui/user-interface.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var test = require('tape');
var jsdom = require('jsdom');
var JSDOM = jsdom.JSDOM;

var DOM = new JSDOM(`<body></body>`);

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();
});

0 comments on commit 8fd4efc

Please sign in to comment.