|
| 1 | +const TwingLoaderChain = require('../../../../../lib/twing/loader/chain').TwingLoaderChain; |
| 2 | +const TwingLoaderArray = require('../../../../../lib/twing/loader/array').TwingLoaderArray; |
| 3 | +const TwingLoaderFilesystem = require('../../../../../lib/twing/loader/filesystem').TwingLoaderFilesystem; |
| 4 | +const TwingErrorLoader = require('../../../../../lib/twing/error/loader').TwingErrorLoader; |
| 5 | + |
| 6 | +const tap = require('tap'); |
| 7 | +const nodePath = require('path'); |
| 8 | +const sinon = require('sinon'); |
| 9 | + |
| 10 | +let fixturesPath = nodePath.resolve('test/tests/integration/fixtures'); |
| 11 | + |
| 12 | +tap.test('loader chain', function (test) { |
| 13 | + test.test('constructor', function(test) { |
| 14 | + test.test('should accept zero parameters', function(test) { |
| 15 | + let loader = new TwingLoaderChain(); |
| 16 | + |
| 17 | + test.ok(loader); |
| 18 | + |
| 19 | + test.end(); |
| 20 | + }); |
| 21 | + |
| 22 | + test.end(); |
| 23 | + }); |
| 24 | + |
| 25 | + test.test('getSourceContext', function (test) { |
| 26 | + let loader = new TwingLoaderChain([ |
| 27 | + new TwingLoaderArray({'foo': 'bar'}), |
| 28 | + new TwingLoaderArray({'errors/index.html': 'baz'}), |
| 29 | + new TwingLoaderFilesystem([fixturesPath]), |
| 30 | + ]); |
| 31 | + |
| 32 | + test.equals(loader.getSourceContext('foo').getName(), 'foo'); |
| 33 | + test.same(loader.getSourceContext('foo').getPath(), ''); |
| 34 | + |
| 35 | + test.equals(loader.getSourceContext('errors/index.html').getName(), 'errors/index.html'); |
| 36 | + test.same(loader.getSourceContext('errors/index.html').getPath(), ''); |
| 37 | + test.equals(loader.getSourceContext('errors/index.html').getCode(), 'baz'); |
| 38 | + |
| 39 | + test.equals(loader.getSourceContext('errors/base.html').getName(), 'errors/base.html'); |
| 40 | + test.equals(nodePath.resolve(loader.getSourceContext('errors/base.html').getPath()), nodePath.join(fixturesPath, 'errors/base.html')); |
| 41 | + test.notEquals(loader.getSourceContext('errors/base.html').getCode(), 'baz'); |
| 42 | + |
| 43 | + test.end(); |
| 44 | + }); |
| 45 | + |
| 46 | + test.test('getSourceContextWhenTemplateDoesNotExist', function (test) { |
| 47 | + let loader = new TwingLoaderChain([]); |
| 48 | + |
| 49 | + test.throws(function () { |
| 50 | + loader.getSourceContext('foo'); |
| 51 | + }, new TwingErrorLoader('Template "foo" is not defined.')); |
| 52 | + |
| 53 | + test.end(); |
| 54 | + }); |
| 55 | + |
| 56 | + test.test('getCacheKey', function (test) { |
| 57 | + let loader = new TwingLoaderChain([ |
| 58 | + new TwingLoaderArray({'foo': 'bar'}), |
| 59 | + new TwingLoaderArray({'foo': 'foobar', 'bar': 'foo'}), |
| 60 | + ]); |
| 61 | + |
| 62 | + test.equals(loader.getCacheKey('foo'), 'foo:bar'); |
| 63 | + test.equals(loader.getCacheKey('bar'), 'bar:foo'); |
| 64 | + |
| 65 | + test.end(); |
| 66 | + }); |
| 67 | + |
| 68 | + test.test('getCacheKeyWhenTemplateDoesNotExist', function (test) { |
| 69 | + let loader = new TwingLoaderChain([]); |
| 70 | + |
| 71 | + test.throws(function () { |
| 72 | + loader.getCacheKey('foo'); |
| 73 | + }, new TwingErrorLoader('Template "foo" is not defined.')); |
| 74 | + |
| 75 | + test.end(); |
| 76 | + }); |
| 77 | + |
| 78 | + test.test('addLoader', function (test) { |
| 79 | + let loader = new TwingLoaderChain([]); |
| 80 | + loader.addLoader(new TwingLoaderArray({'foo': 'bar'})); |
| 81 | + |
| 82 | + test.equals(loader.getSourceContext('foo').getCode(), 'bar'); |
| 83 | + |
| 84 | + test.end(); |
| 85 | + }); |
| 86 | + |
| 87 | + test.test('exists', function (test) { |
| 88 | + let loader1 = new TwingLoaderArray({}); |
| 89 | + sinon.stub(loader1, 'exists').returns(false); |
| 90 | + sinon.stub(loader1, 'getSourceContext'); |
| 91 | + |
| 92 | + let loader2 = new TwingLoaderArray({}); |
| 93 | + sinon.stub(loader2, 'exists').returns(true); |
| 94 | + sinon.stub(loader2, 'getSourceContext'); |
| 95 | + |
| 96 | + let loader = new TwingLoaderChain([]); |
| 97 | + loader.addLoader(loader1); |
| 98 | + loader.addLoader(loader2); |
| 99 | + |
| 100 | + test.true(loader.exists('foo')); |
| 101 | + |
| 102 | + sinon.assert.calledOnce(loader1['exists']); |
| 103 | + sinon.assert.notCalled(loader1['getSourceContext']); |
| 104 | + |
| 105 | + sinon.assert.calledOnce(loader2['exists']); |
| 106 | + sinon.assert.notCalled(loader2['getSourceContext']); |
| 107 | + |
| 108 | + test.end(); |
| 109 | + }); |
| 110 | + |
| 111 | + test.end(); |
| 112 | +}); |
0 commit comments