From 9cff4ce9f55c288a568513a93df8099a1d871ec3 Mon Sep 17 00:00:00 2001 From: Luke Sneeringer Date: Fri, 9 Dec 2016 01:34:15 -0600 Subject: [PATCH] Forgot to git add the tests file. Grumble. --- package.json | 2 +- src/test/immutables.js | 69 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/test/immutables.js diff --git a/package.json b/package.json index 3bc463e..6627016 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "typed-immutable", - "version": "0.1.0", + "version": "0.1.1", "description": "Immutable structurally typed data", "author": "Irakli Gozalishvili (http://jeditoolkit.com)", "homepage": "https://github.com/typed-immutable/typed-immutable", diff --git a/src/test/immutables.js b/src/test/immutables.js new file mode 100644 index 0000000..d53eb2e --- /dev/null +++ b/src/test/immutables.js @@ -0,0 +1,69 @@ +import * as Immutable from 'immutable' + +import test from './test' +import {Record} from '../record' + + +test('can use Immutable.js objects in records', assert => { + var X = Record({ + a: new Immutable.Map({foo: 'bar'}), + }) + + var x1 = new X() + assert.equals(x1.a.get('foo'), 'bar') + assert.equals(x1.a.get('baz'), undefined) + assert.equals(x1.a.get('baz', 'bacon'), 'bacon') + + var x2 = new X({ + a: new Immutable.Map({spam: 'eggs'}), + }) + assert.equals(x2.a.get('spam'), 'eggs') + assert.equals(x2.a.get('foo'), undefined) + assert.equals(x2.a.get('baz', 'bacon'), 'bacon') + + var x3 = new X({ + a: {spam: 'eggs'}, + }) + assert.equals(x3.a instanceof Immutable.Map, true) + assert.equals(x3.a.get('spam'), 'eggs') + assert.equals(x3.a.get('foo'), undefined) + assert.equals(x3.a.get('baz', 'bacon'), 'bacon') +}) + + +test('can use Immutable.js classes in records', assert => { + var X = Record({ + a: Immutable.Map, + }) + + assert.throws(_ => { + var x1 = new X() + }) + + var x2 = new X({ + a: new Immutable.Map({spam: 'eggs'}), + }) + assert.equals(x2.a.get('spam'), 'eggs') + assert.equals(x2.a.get('foo'), undefined) + assert.equals(x2.a.get('baz', 'bacon'), 'bacon') + + var x3 = new X({ + a: {spam: 'eggs'}, + }) + assert.equals(x3.a instanceof Immutable.Map, true) + assert.equals(x3.a.get('spam'), 'eggs') + assert.equals(x3.a.get('foo'), undefined) + assert.equals(x3.a.get('baz', 'bacon'), 'bacon') +}) + + +test('sets work too', assert => { + var Y = Record({s: new Immutable.Set(['foo'])}) + assert.equals(new Y().s.has('foo'), true) + assert.equals(new Y().s.has('bar'), false) + + var y2 = new Y({s: ['foo', 'bar', 'baz']}) + assert.equals(y2.s.has('baz'), true) + + assert.throws(() => { var y3 = new Y({s: null}) }) +})