Skip to content

Instantly share code, notes, and snippets.

@mprokopowicz
Created July 17, 2018 13:26
Show Gist options
  • Save mprokopowicz/da9b9548f2a702629d5209cc4c6517fe to your computer and use it in GitHub Desktop.
Save mprokopowicz/da9b9548f2a702629d5209cc4c6517fe to your computer and use it in GitHub Desktop.
'use strict';
const { expect } = require('chai');
const Context = require('../../lib/sharedContext/Context');
describe('Context', () => {
let ctx;
let name;
let notExistingName;
let initialValue;
let newValue;
beforeEach(() => {
ctx = new Context();
name = 'testProperty';
notExistingName = 'nope';
initialValue = { deed: 'beef' };
newValue = { foo: 'bar' };
});
describe('#declare', () => {
it('should register new read only context property', () => {
ctx.declare(name, initialValue, ctx.READ_ONLY);
expect(ctx.has(name)).to.be.ok;
expect(() => ctx.update(name, newValue)).to.throw(Context.PropertyNotWritableError);
expect(() => ctx.delete(name)).to.throw(Context.PropertyNotDeletableError);
});
it('should register new writable context property', () => {
ctx.declare(name, initialValue, ctx.WRITABLE);
expect(ctx.has(name)).to.be.ok;
expect(() => ctx.update(name, newValue)).not.to.throw();
expect(() => ctx.delete(name)).to.throw(Context.PropertyNotDeletableError);
expect(ctx.get(name)).to.deep.equal(newValue);
});
it('should register new writable and deletable property', () => {
ctx.declare(name, initialValue, ctx.WRITABLE | ctx.DELETABLE);
expect(ctx.has(name)).to.be.ok;
expect(() => ctx.update(name, newValue)).not.to.throw();
expect(ctx.get(name)).to.deep.equal(newValue);
expect(() => ctx.delete(name)).not.to.throw();
expect(ctx.has(name)).not.to.be.ok;
expect(ctx.get(name)).to.be.null;
});
it('should have writable and deletable mode by default', () => {
ctx.declare(name, initialValue);
expect(ctx.has(name)).to.be.ok;
expect(() => ctx.update(name, newValue)).not.to.throw();
expect(ctx.get(name)).to.deep.equal(newValue);
expect(() => ctx.delete(name)).not.to.throw();
expect(ctx.has(name)).not.to.be.ok;
expect(ctx.get(name)).to.be.null;
});
});
describe('#get', () => {
it('should return stored value', () => {
ctx.declare(name, initialValue);
expect(ctx.get(name)).to.deep.equal(initialValue);
});
it('should return fallback value if property does not exists', () => {
expect(ctx.get(notExistingName, { a: 1 })).to.deep.equal({ a: 1 });
});
it('should return null if property does not exists and no default value was provided', () => {
expect(ctx.get(notExistingName)).to.be.null;
});
it('should return a copy of stored data', () => {
ctx.declare(name, initialValue);
const retrived = ctx.get(name);
retrived.newProp = { a: 2 };
delete retrived.deed;
expect(ctx.get(name)).to.deep.equal({ deed: 'beef' });
});
});
describe('update', () => {
it('should throw if property is not declared', () => {
expect(() => ctx.update(notExistingName)).to.throw(Context.PropertyNotDeclaredError);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment