Skip to content

Commit 47a7311

Browse files
committed
fix(smalltalk) options present, cancel missing
1 parent 282f54d commit 47a7311

File tree

3 files changed

+171
-7
lines changed

3 files changed

+171
-7
lines changed

lib/smalltalk.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function showDialog(title, msg, value, buttons, options) {
9898
];
9999

100100
const promise = new Promise((resolve, reject) => {
101-
const noCancel = options && !options.cancel;
101+
const noCancel = options && options.cancel === false;
102102
const empty = () => {};
103103

104104
ok(resolve);
@@ -120,9 +120,9 @@ function showDialog(title, msg, value, buttons, options) {
120120
el.setSelectionRange(0, value.length);
121121
});
122122

123-
addListenerAll('click', dialog, closeButtons, (event) =>
123+
addListenerAll('click', dialog, closeButtons, (event) => {
124124
closeDialog(event.target, dialog, ok(), cancel())
125-
);
125+
});
126126

127127
['click', 'contextmenu'].forEach((event) =>
128128
dialog.addEventListener(event, () =>

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@
6464
"tape": "^4.6.0",
6565
"url-loader": "^1.0.1",
6666
"webpack": "^4.1.0",
67-
"webpack-cli": "^3.0.8"
67+
"webpack-cli": "^3.0.8",
68+
"wraptile": "^2.0.0"
6869
},
6970
"dependencies": {
7071
"currify": "^2.0.3",

test/smalltalk.js

Lines changed: 166 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require('css-modules-require-hook/preset');
88
const test = require('tape');
99
const sinon = require('sinon');
1010
const currify = require('currify');
11+
const wraptile = require('wraptile');
1112

1213
global.window = {};
1314

@@ -460,11 +461,12 @@ test('smalltalk: confirm: innerHTML', (t) => {
460461
global.document.createElement = createElement;
461462

462463
smalltalk.confirm('title', 'message');
463-
t.equal(fixture.confirm(), el.innerHTML, 'should be equal');
464464

465465
fixture.confirm.update(el.innerHTML);
466466

467467
after();
468+
469+
t.equal(fixture.confirm(), el.innerHTML, 'should be equal');
468470
t.end();
469471
});
470472

@@ -849,7 +851,6 @@ test('smalltalk: prompt: click on ok', (t) => {
849851

850852
const createElement = getCreateElement(el);
851853
document.createElement = createElement;
852-
853854
document.querySelector = () => el;
854855

855856
smalltalk.prompt('title', 'message', value)
@@ -866,6 +867,168 @@ test('smalltalk: prompt: click on ok', (t) => {
866867
});
867868
});
868869

870+
test('smalltalk: prompt: click on cancel', (t) => {
871+
before();
872+
873+
const dataName = (a) => `[data-name="js-${a}"]`;
874+
const noop = () => {};
875+
876+
const value = 'hello';
877+
const input = {
878+
value,
879+
focus: noop,
880+
setSelectionRange: noop,
881+
};
882+
883+
const cancel = {
884+
getAttribute: () => 'js-cancel',
885+
focus: sinon.stub(),
886+
addEventListener: sinon.stub(),
887+
};
888+
889+
const querySelector = (a) => {
890+
if (a === dataName('input'))
891+
return input;
892+
893+
if (a === dataName('cancel'))
894+
return cancel;
895+
};
896+
897+
const el = {
898+
querySelector,
899+
parentElement: {
900+
removeChild: () => {}
901+
},
902+
};
903+
904+
const createElement = getCreateElement(el);
905+
document.createElement = createElement;
906+
document.querySelector = () => el;
907+
908+
smalltalk.prompt('title', 'message', value)
909+
.catch(() => {
910+
after();
911+
t.pass('should reject');
912+
t.end();
913+
});
914+
915+
const [, close] = cancel.addEventListener.args.pop();
916+
917+
close({
918+
target: cancel
919+
});
920+
});
921+
922+
test('smalltalk: prompt: click on cancel: cancel false', (t) => {
923+
before();
924+
925+
const dataName = (a) => `[data-name="js-${a}"]`;
926+
const noop = () => {};
927+
928+
const value = 'hello';
929+
const input = {
930+
value,
931+
focus: noop,
932+
setSelectionRange: noop,
933+
};
934+
935+
const cancel = {
936+
getAttribute: () => 'js-cancel',
937+
focus: sinon.stub(),
938+
addEventListener: sinon.stub(),
939+
};
940+
941+
const querySelector = (a) => {
942+
if (a === dataName('input'))
943+
return input;
944+
945+
if (a === dataName('cancel'))
946+
return cancel;
947+
};
948+
949+
const el = {
950+
querySelector,
951+
parentElement: {
952+
removeChild: () => {}
953+
},
954+
};
955+
956+
const createElement = getCreateElement(el);
957+
document.createElement = createElement;
958+
document.querySelector = () => el;
959+
960+
const fail = t.fail.bind(t);
961+
const end = t.end.bind(t);
962+
963+
smalltalk.prompt('title', 'message', value, {cancel: false})
964+
.then(wraptile(fail, 'should not pass'))
965+
.catch(wraptile(fail, 'should not reject'))
966+
.then(end);
967+
968+
const [, close] = cancel.addEventListener.args.pop();
969+
970+
close({
971+
target: cancel
972+
});
973+
974+
t.pass('should do nothing');
975+
t.end();
976+
977+
after();
978+
});
979+
980+
test('smalltalk: prompt: click on cancel: options: no cancel', (t) => {
981+
before();
982+
983+
const dataName = (a) => `[data-name="js-${a}"]`;
984+
const noop = () => {};
985+
986+
const value = 'hello';
987+
const input = {
988+
value,
989+
focus: noop,
990+
setSelectionRange: noop,
991+
};
992+
993+
const cancel = {
994+
getAttribute: () => 'js-cancel',
995+
focus: sinon.stub(),
996+
addEventListener: sinon.stub(),
997+
};
998+
999+
const querySelector = (a) => {
1000+
if (a === dataName('input'))
1001+
return input;
1002+
1003+
if (a === dataName('cancel'))
1004+
return cancel;
1005+
};
1006+
1007+
const el = {
1008+
querySelector,
1009+
parentElement: {
1010+
removeChild: () => {}
1011+
},
1012+
};
1013+
1014+
const createElement = getCreateElement(el);
1015+
document.createElement = createElement;
1016+
document.querySelector = () => el;
1017+
1018+
smalltalk.prompt('title', 'message', value, {})
1019+
.catch(() => {
1020+
after();
1021+
t.pass('should reject');
1022+
t.end();
1023+
});
1024+
1025+
const [, close] = cancel.addEventListener.args.pop();
1026+
1027+
close({
1028+
target: cancel
1029+
});
1030+
});
1031+
8691032
test('smalltalk: prompt: custom label', (t) => {
8701033
before();
8711034

@@ -877,7 +1040,7 @@ test('smalltalk: prompt: custom label', (t) => {
8771040
global.document.createElement = createElement;
8781041

8791042
const options = {
880-
buttons :{
1043+
buttons: {
8811044
ok: 'Ok',
8821045
cancel: 'Logout',
8831046
},

0 commit comments

Comments
 (0)