123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- 'use strict';
- var expect = require('chai').expect;
- var Refs = require('../../');
- var hasProperty = require('./has-property');
- describe('refs', function() {
- describe('one-to-one', function() {
- var refs = new Refs({ name: 'foo' }, { name: 'bar' });
- it('should keep already set property', function() {
- // given
- var b = { };
- var a = { foo: b };
- // when
- refs.bind(a, 'foo');
- // then
- expect(a.foo).to.equal(b);
- });
- it('should define property invisibly', function() {
- // given
- var a = {};
- // when
- refs.bind(a, 'foo');
- // then
- expect(a.foo).not.to.exist;
- expect(hasProperty(a, 'foo')).to.equal(true);
- });
- it('should define non-enumerable property per default', function() {
- // given
- var a = {};
- refs.bind(a, 'foo');
- a.foo = {};
- // when
- var json = JSON.stringify(a);
- expect(json).to.equal('{}');
- });
- it('should define enumerable property', function() {
- // given
- var enumerableRefs = new Refs({ name: 'foo', enumerable: true }, { name: 'bar' });
- var a = {};
- enumerableRefs.bind(a, 'foo');
- a.foo = {};
- // when
- var json = JSON.stringify(a);
- expect(json).to.equal('{"foo":{}}');
- });
- it('should define non-configurable property per default', function() {
- // given
- var refs = new Refs({ name: 'foo' }, { name: 'bar' });
- var a = {};
- refs.bind(a, 'foo');
- a.foo = {};
- // when
- expect(function() {
- delete a.foo;
- }).to.throw;
- });
- it('should define configurable property', function() {
- // given
- var configurableRefs = new Refs(
- { name: 'foo', configurable: true },
- { name: 'bar' }
- );
- var a = {};
- configurableRefs.bind(a, 'foo');
- a.foo = {};
- // when
- delete a.foo;
- expect(a.foo).to.not.exist;
- });
- it('should create bi-directional configurable property', function() {
- // given
- var configurableRefs = new Refs(
- { name: 'foo', configurable: true },
- { name: 'bar', configurable: true }
- );
- var a = {};
- configurableRefs.bind(a, 'foo');
- a.foo = {};
- // when
- delete a.foo.bar;
- expect(a.foo.bar).to.not.exist;
- });
- it('should create bi-directional reference', function() {
- // given
- var a = {}, b = {};
- refs.bind(a, 'foo');
- refs.bind(b, 'bar');
- // when
- a.foo = b;
- // then
- expect(a.foo).to.equal(b);
- expect(b.bar).to.equal(a);
- });
- it('should unset property', function() {
- // given
- var a = {}, b = {};
- refs.bind(a, 'foo');
- refs.bind(b, 'bar');
- // when
- a.foo = b;
- a.foo = null;
- expect(a.foo).to.equal(null);
- expect(b.bar).to.equal(undefined);
- });
- it('should transitively define properties', function() {
- // given
- var a = {}, b = {};
- refs.bind(a, 'foo');
- // when
- a.foo = b;
- b.bar = null;
- // then
- expect(a.foo).to.equal(undefined);
- });
- it('should not allow property deletion', function() {
- // given
- var a = {};
- refs.bind(a, 'foo');
- expect(function() {
- // when
- delete a.foo;
- }).to.throw('Cannot delete property \'foo\' of #<Object>');
- });
- });
- describe('one-to-many', function() {
- var refs = new Refs({ name: 'foos', collection: true }, { name: 'bar' });
- it('should define non-enumerable property per default', function() {
- // given
- var a = {};
- refs.bind(a, 'foos');
- a.foos.push({});
- // when
- var json = JSON.stringify(a);
- expect(json).to.equal('{}');
- });
- it('should define enumerable property', function() {
- // given
- var enumerableRefs = new Refs({ name: 'foos', enumerable: true, collection: true }, { name: 'bar' });
- var a = {};
- enumerableRefs.bind(a, 'foos');
- a.foos.push({});
- // when
- var json = JSON.stringify(a);
- expect(json).to.equal('{"foos":[{}]}');
- });
- it('should keep already set property', function() {
- // given
- var b = { };
- var a = { foos: [ b ] };
- // when
- refs.bind(a, 'foos');
- // then
- expect(a.foos).to.eql([ b ]);
- });
- it('should define collection property as array', function() {
- // given
- var a = {};
- // when
- refs.bind(a, 'foos');
- // then
- expect(a.foos).to.eql([]);
- });
- it('should auto-populate collection', function() {
- // given
- var a = {};
- var b1 = {}, b2 = {};
- refs.bind(b1, 'bar');
- refs.bind(b2, 'bar');
- // when
- b1.bar = a;
- b2.bar = a;
- // then
- expect(a.foos).to.eql([ b1, b2 ]);
- });
- it('should auto-remove from collection', function() {
- // given
- var a = {};
- var b1 = {}, b2 = {};
- refs.bind(b1, 'bar');
- refs.bind(b2, 'bar');
- b1.bar = a;
- b2.bar = a;
- // when
- b1.bar = null;
- // then
- expect(a.foos).to.eql([ b2 ]);
- });
- it('should transitively bind one property', function() {
- // given
- var a = {}, b = {};
- refs.bind(a, 'foos');
- // when
- a.foos.add(b);
- b.bar = null;
- // then
- expect(a.foos).to.eql([]);
- });
- it('should transitively bind many property', function() {
- // given
- var b = { };
- var c = { };
- var a = { foos: [ c ] };
- refs.bind(b, 'bar');
- // when
- b.bar = a;
- // then
- expect(a.foos).to.eql([ c, b ]);
- });
- it('should transitively bind many-to-one inverse reference', function() {
- // given
- var b = { };
- var c = { };
- var a = { foos: [ b, c ] };
- // when
- refs.bind(a, 'foos');
- // then
- expect(b.bar).to.eql(a);
- expect(c.bar).to.eql(a);
- expect(a.foos).to.eql([ b, c ]);
- });
- });
- describe('many-to-many', function() {
- var refs = new Refs({ name: 'foos', collection: true }, { name: 'bars', collection: true });
- it('should define property invisibly', function() {
- // given
- var b = {};
- // when
- refs.bind(b, 'bars');
- // then
- expect(b.bars).to.eql([]);
- });
- it('should inverse add', function() {
- // given
- var a = {}, b = {};
- refs.bind(a, 'foos');
- refs.bind(b, 'bars');
- // when
- a.foos.add(b);
- // then
- expect(b.bars).to.eql([ a ]);
- });
- it('should add many-to-many', function() {
- // given
- var a1 = {}, a2 = {},
- b1 = {}, b2 = {};
- refs.bind(a1, 'foos');
- refs.bind(a2, 'foos');
- // when
- a1.foos.add(b1);
- a1.foos.add(b2);
- a2.foos.add(b2);
- // then
- expect(b2.bars.contains(a2)).to.equal(true);
- expect(b1.bars).to.eql([ a1 ]);
- expect(b2.bars).to.eql([ a1, a2 ]);
- });
- it('should inverse remove', function() {
- // given
- var a1 = {}, a2 = {},
- b1 = {}, b2 = {};
- refs.bind(a1, 'foos');
- refs.bind(a2, 'foos');
- // when
- a1.foos.add(b1);
- a1.foos.add(b2);
- a2.foos.add(b2);
- b2.bars.remove(a1);
- // then
- expect(b1.bars).to.eql([ a1 ]);
- expect(b2.bars).to.eql([ a2 ]);
- });
- it('should transitively define properties', function() {
- // given
- var a = {}, b = {};
- refs.bind(a, 'foos');
- // when
- a.foos.add(b);
- b.bars.remove(a);
- // then
- expect(a.foos).to.eql([]);
- expect(b.bars).to.eql([]);
- });
- });
- });
|