context-test.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. require('./helper');
  2. var Context = Mustache.Context;
  3. describe('A new Mustache.Context', function () {
  4. var context;
  5. beforeEach(function () {
  6. context = new Context({ name: 'parent', message: 'hi', a: { b: 'b' } });
  7. });
  8. it('is able to lookup properties of its own view', function () {
  9. assert.equal(context.lookup('name'), 'parent');
  10. });
  11. it('is able to lookup nested properties of its own view', function () {
  12. assert.equal(context.lookup('a.b'), 'b');
  13. });
  14. describe('when pushed', function () {
  15. beforeEach(function () {
  16. context = context.push({ name: 'child', c: { d: 'd' } });
  17. });
  18. it('returns the child context', function () {
  19. assert.equal(context.view.name, 'child');
  20. assert.equal(context.parent.view.name, 'parent');
  21. });
  22. it('is able to lookup properties of its own view', function () {
  23. assert.equal(context.lookup('name'), 'child');
  24. });
  25. it("is able to lookup properties of the parent context's view", function () {
  26. assert.equal(context.lookup('message'), 'hi');
  27. });
  28. it('is able to lookup nested properties of its own view', function () {
  29. assert.equal(context.lookup('c.d'), 'd');
  30. });
  31. it('is able to lookup nested properties of its parent view', function () {
  32. assert.equal(context.lookup('a.b'), 'b');
  33. });
  34. });
  35. });
  36. describe('A Mustache.Context', function () {
  37. var context;
  38. describe('with an empty string in the lookup chain', function () {
  39. var view, context;
  40. beforeEach(function () {
  41. view = { a: '' };
  42. view.a.b = 'value';
  43. context = new Context(view);
  44. });
  45. it('is able to lookup a nested property', function () {
  46. assert.equal(context.lookup('a.b'), view.a.b);
  47. });
  48. });
  49. });