|
| 1 | +import test from 'tape'; |
| 2 | + |
| 3 | +import { flying, createDuck } from './index'; |
| 4 | + |
| 5 | +// What is a functional mixin? |
| 6 | +/* |
| 7 | +flying(o) => m: o & { |
| 8 | + fly() => m, |
| 9 | + isFlying() => Boolean, |
| 10 | + land() => m |
| 11 | +} |
| 12 | +*/ |
| 13 | +test('flying.fly() and .isFlying()', assert => { |
| 14 | + const msg = 'should set isFlying() to true'; |
| 15 | + const o = flying({}); |
| 16 | + |
| 17 | + const actual = o.fly().isFlying(); |
| 18 | + const expected = true; |
| 19 | + |
| 20 | + assert.same(actual, expected, msg); |
| 21 | + assert.end(); |
| 22 | +}); |
| 23 | + |
| 24 | +test('flying.land()', assert => { |
| 25 | + const msg = 'should set isFlying() to false'; |
| 26 | + const o = flying({}).fly(); |
| 27 | + assert.same(o.isFlying(), true, 'starts out flying'); |
| 28 | + |
| 29 | + const actual = o.land().isFlying(); |
| 30 | + const expected = false; |
| 31 | + |
| 32 | + assert.same(actual, expected, msg); |
| 33 | + assert.end(); |
| 34 | +}); |
| 35 | + |
| 36 | +test('flying() mixin', assert => { |
| 37 | + const msg = 'should mix into existing object'; |
| 38 | + const startingObject = { a: 'a' }; |
| 39 | + |
| 40 | + const actual = flying(startingObject).a; |
| 41 | + const expected = 'a'; |
| 42 | + |
| 43 | + assert.same(actual, expected, msg); |
| 44 | + assert.end(); |
| 45 | +}); |
| 46 | + |
| 47 | +test('createDuck()', assert => { |
| 48 | + const msg = 'should have FLYING and quacking'; |
| 49 | + const quack = 'QUACK!'; |
| 50 | + const duck = createDuck(quack); |
| 51 | + |
| 52 | + const actual = duck.fly().quack(); |
| 53 | + const expected = quack; |
| 54 | + |
| 55 | + assert.same(actual, expected, msg); |
| 56 | + assert.end(); |
| 57 | +}); |
| 58 | + |
| 59 | +// When should you use functional mixins? |
| 60 | +// * state / data privacy |
0 commit comments