Skip to content

Commit 10c91cd

Browse files
committed
test(smalltalk) add ability to update fixtures
1 parent 6cf7fc6 commit 10c91cd

File tree

3 files changed

+47
-56
lines changed

3 files changed

+47
-56
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"lint:js": "eslint lib test webpack.config.js",
3030
"lint": "redrun lint:*",
3131
"test": "tape 'test/**/*.js'",
32+
"test:update": "UPDATE_FIXTURE=1 redrun test",
3233
"init": "mkdirp native"
3334
},
3435
"keywords": [

test/fixture/prompt-no-password.html

Lines changed: 0 additions & 18 deletions
This file was deleted.

test/smalltalk.js

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,27 @@ require('css-modules-require-hook/preset');
77

88
const test = require('tape');
99
const sinon = require('sinon');
10+
const currify = require('currify');
1011

1112
global.window = {};
1213

14+
const {UPDATE_FIXTURE} = process.env;
15+
const isUpdateFixtures = UPDATE_FIXTURE === 'true' || UPDATE_FIXTURE === '1';
16+
const noop = () => {};
17+
1318
const smalltalk = require('../lib/smalltalk');
1419
const fixtureDir = path.join(__dirname, 'fixture');
15-
const readFixture = (name) => {
16-
return fs.readFileSync(`${fixtureDir}/${name}.html`, 'utf8');
17-
};
1820

1921
const writeFixture = (name, data) => {
2022
return fs.writeFileSync(`${fixtureDir}/${name}.html`, data);
2123
};
2224

25+
const readFixture = (name) => {
26+
const fn = () => fs.readFileSync(`${fixtureDir}/${name}.html`, 'utf8');
27+
fn.update = !isUpdateFixtures ? noop : currify(writeFixture, name);
28+
return fn;
29+
};
30+
2331
const fixture = {
2432
alert: readFixture('alert'),
2533
confirm: readFixture('confirm'),
@@ -42,8 +50,8 @@ test('smalltalk: alert: innerHTML', (t) => {
4250
global.document.createElement = createElement;
4351

4452
smalltalk.alert('title', 'hello\nworld');
45-
t.equal(fixture.alert, el.innerHTML, 'should be equal');
46-
writeFixture('alert', el.innerHTML);
53+
t.equal(fixture.alert(), el.innerHTML, 'should be equal');
54+
fixture.alert.update(el.innerHTML);
4755

4856
after();
4957
t.end();
@@ -123,9 +131,9 @@ test('smalltalk: alert: close: querySelector', (t) => {
123131
target: ok
124132
});
125133

126-
t.equal(querySelector.args.pop().pop(), '.smalltalk', 'should find smalltalk');
127-
128134
after();
135+
136+
t.equal(querySelector.args.pop().pop(), '.smalltalk', 'should find smalltalk');
129137
t.end();
130138
});
131139

@@ -164,9 +172,9 @@ test('smalltalk: alert: close: remove', (t) => {
164172
target: ok
165173
});
166174

167-
t.equal(parentElement.removeChild.args.pop().pop(), el, 'should find smalltalk');
168-
169175
after();
176+
177+
t.equal(parentElement.removeChild.args.pop().pop(), el, 'should find smalltalk');
170178
t.end();
171179
});
172180

@@ -208,10 +216,9 @@ test('smalltalk: alert: keydown: stopPropagation', (t) => {
208216
};
209217

210218
keydown(event);
219+
after();
211220

212221
t.ok(event.stopPropagation.called, 'should call stopPropagation');
213-
214-
after();
215222
t.end();
216223
});
217224

@@ -259,10 +266,9 @@ test('smalltalk: alert: keydown: tab: preventDefault', (t) => {
259266
};
260267

261268
keydown(event);
269+
after();
262270

263271
t.ok(event.preventDefault.called, 'should call preventDefault');
264-
265-
after();
266272
t.end();
267273
});
268274

@@ -312,10 +318,9 @@ test('smalltalk: alert: keydown: tab: active name', (t) => {
312318
};
313319

314320
keydown(event);
321+
after();
315322

316323
t.ok(event.preventDefault.called, 'should call preventDefault');
317-
318-
after();
319324
t.end();
320325
});
321326

@@ -365,9 +370,9 @@ test('smalltalk: alert: keydown: left: focus', (t) => {
365370

366371
keydown(event);
367372

368-
t.ok(focus.called, 'should call focus');
369-
370373
after();
374+
375+
t.ok(focus.called, 'should call focus');
371376
t.end();
372377
});
373378

@@ -413,10 +418,9 @@ test('smalltalk: alert: click', (t) => {
413418
};
414419

415420
keydown(event);
421+
after();
416422

417423
t.ok(focus.called, 'should call focus');
418-
419-
after();
420424
t.end();
421425
});
422426

@@ -437,10 +441,11 @@ test('smalltalk: alert: custom label', (t) => {
437441
};
438442

439443
smalltalk.alert('title', 'hello\nworld', options);
444+
after();
440445

441-
t.equal(fixture.alertCustomLabel, el.innerHTML, 'should be equal');
446+
fixture.alertCustomLabel.update(el.innerHTML);
442447

443-
after();
448+
t.equal(fixture.alertCustomLabel(), el.innerHTML, 'should be equal');
444449
t.end();
445450
});
446451

@@ -455,8 +460,9 @@ test('smalltalk: confirm: innerHTML', (t) => {
455460
global.document.createElement = createElement;
456461

457462
smalltalk.confirm('title', 'message');
458-
t.equal(fixture.confirm, el.innerHTML, 'should be equal');
459-
writeFixture('confirm', el.innerHTML);
463+
t.equal(fixture.confirm(), el.innerHTML, 'should be equal');
464+
465+
fixture.confirm.update(el.innerHTML);
460466

461467
after();
462468
t.end();
@@ -489,8 +495,8 @@ test('smalltalk: confirm: click on close', (t) => {
489495

490496
smalltalk.confirm('title', 'message')
491497
.catch(() => {
492-
t.pass('should reject');
493498
after();
499+
t.pass('should reject');
494500
t.end();
495501
});
496502

@@ -556,10 +562,9 @@ test('smalltalk: confirm: keydown: left: active name', (t) => {
556562
};
557563

558564
keydown(event);
565+
after();
559566

560567
t.ok(focus.called, 'should call focus');
561-
562-
after();
563568
t.end();
564569
});
565570

@@ -618,10 +623,9 @@ test('smalltalk: confirm: keydown: left: active name: cancel', (t) => {
618623
};
619624

620625
keydown(event);
626+
after();
621627

622628
t.ok(focus.called, 'should call focus');
623-
624-
after();
625629
t.end();
626630
});
627631

@@ -655,8 +659,8 @@ test('smalltalk: confirm: keydown: esc: reject', (t) => {
655659

656660
smalltalk.confirm('title', 'message')
657661
.catch(() => {
658-
t.pass('should reject');
659662
after();
663+
t.pass('should reject');
660664
t.end();
661665
});
662666

@@ -746,8 +750,9 @@ test('smalltalk: confirm: custom label', (t) => {
746750

747751
smalltalk.confirm('title', 'message', options);
748752
after();
753+
fixture.confirmCustomLabel.update(el.innerHTML);
749754

750-
t.equal(fixture.confirmCustomLabel, el.innerHTML, 'should be equal');
755+
t.equal(fixture.confirmCustomLabel(), el.innerHTML, 'should be equal');
751756
t.end();
752757
});
753758

@@ -762,8 +767,8 @@ test('smalltalk: prompt: innerHTML', (t) => {
762767
global.document.createElement = createElement;
763768

764769
smalltalk.prompt('title', 'message', 2);
765-
t.equal(fixture.prompt, el.innerHTML, 'should be equal');
766-
writeFixture('prompt', el.innerHTML);
770+
t.equal(fixture.prompt(), el.innerHTML, 'should be equal');
771+
fixture.prompt.update(el.innerHTML);
767772

768773
after();
769774
t.end();
@@ -783,9 +788,10 @@ test('smalltalk: prompt: password', (t) => {
783788
type: 'password'
784789
});
785790

786-
t.equal(fixture.promptPassword, el.innerHTML, 'should be equal');
787-
791+
fixture.promptPassword.update(el.innerHTML);
788792
after();
793+
794+
t.equal(fixture.promptPassword(), el.innerHTML, 'should be equal');
789795
t.end();
790796
});
791797

@@ -800,9 +806,10 @@ test('smalltalk: prompt: no value', (t) => {
800806
global.document.createElement = createElement;
801807

802808
smalltalk.prompt('title', 'message');
803-
t.equal(fixture.promptNoValue, el.innerHTML, 'should be equal');
804-
805809
after();
810+
fixture.promptNoValue.update(el.innerHTML);
811+
812+
t.equal(fixture.promptNoValue(), el.innerHTML, 'should be equal');
806813
t.end();
807814
});
808815

@@ -847,8 +854,8 @@ test('smalltalk: prompt: click on ok', (t) => {
847854

848855
smalltalk.prompt('title', 'message', value)
849856
.then((result) => {
850-
t.equal(result, value, 'should return value');
851857
after();
858+
t.equal(result, value, 'should return value');
852859
t.end();
853860
});
854861

@@ -878,8 +885,9 @@ test('smalltalk: prompt: custom label', (t) => {
878885

879886
smalltalk.prompt('title', 'message', 2, options);
880887
after();
888+
fixture.promptCustomLabel.update();
881889

882-
t.equal(fixture.promptCustomLabel, el.innerHTML, 'should be equal');
890+
t.equal(fixture.promptCustomLabel(), el.innerHTML, 'should be equal');
883891
t.end();
884892
});
885893

0 commit comments

Comments
 (0)