it('emits `error(request, error)` event on template error', done => { const onTemplateError = sinon.spy(); mockTemplate.returns(false); tailor.on('error', onTemplateError); http.get('http://localhost:8080/template', response => { const request = onTemplateError.args[0][0]; const error = onTemplateError.args[0][1]; assert.equal(request.url, '/template'); assert.equal(error, 'Error fetching template'); response.resume(); response.on('end', done); }); });
it('forwards `fragment:start(request, fragment)` event from a fragment', done => { const onFragmentStart = sinon.spy(); nock('https://fragment') .get('/') .reply(200, 'hello'); mockTemplate.returns('<fragment src="https://fragment">'); tailor.on('fragment:start', onFragmentStart); http.get('http://localhost:8080/template', response => { const request = onFragmentStart.args[0][0]; const fragment = onFragmentStart.args[0][1]; assert.equal(request.url, '/template'); assert.equal(fragment.url, 'https://fragment'); response.resume(); response.on('end', done); }); });
test('works with MemberExpression', () => { const mockBody = {}; const mockProperty = {}; const mockPushState = sinon.spy(); const mockPopState = sinon.spy(); const mockScan = sinon.spy(); const mockInstance = { scan: mockScan, pushState: mockPushState, popState: mockPopState, }; FunctionTracer.prototype.scan.call(mockInstance, { type: 'MemberExpression', object: mockBody, property: mockProperty }); assert.ok(mockScan.called); assert.equal(mockScan.args[0][0], mockBody); assert.equal(mockScan.args[1][0], mockProperty); assert.equal(mockPushState.args[0][0], 'memberExpression'); assert.equal(mockPopState.args[0][0], 'memberExpression'); });
it('emits `context:error(request, error)` event', done => { const onContextError = sinon.spy(); const rejectPrm = Promise.reject('Error fetching context'); rejectPrm.catch(() => {}); mockContext.returns(rejectPrm); tailor.on('context:error', onContextError); http.get('http://localhost:8080/template', response => { const request = onContextError.args[0][0]; const error = onContextError.args[0][1]; assert.equal(request.url, '/template'); assert.equal(error, 'Error fetching context'); response.resume(); response.on('end', done); }); });
test('works with Identifier', () => { const mockCurrentContext = {}; const mockIsState = sinon.spy(); const mockGetDeclaration = sinon.spy(() => 123); const mockInstance = { identifiers: [], currentContext: mockCurrentContext, isState: mockIsState, getDeclaration: mockGetDeclaration, }; const mockAst = { type: 'Identifier', name: 'x' }; FunctionTracer.prototype.scan.call(mockInstance, mockAst); assert.ok(mockGetDeclaration.called); assert.equal(mockGetDeclaration.args[0][0], 'x'); assert.deepEqual(mockInstance.identifiers, [ { context: mockInstance.currentContext, ast: mockAst, declaration: 123 } ]); assert.equal(mockIsState.args[0][0], 'trackIdentifiers'); });
GLKernel.prototype.setOutput.call(mockInstance, [100, 100]); assert.equal(mockContext.bindFramebuffer.callCount, 1); assert.equal(mockContext.bindFramebuffer.args[0][0], 'FRAMEBUFFER'); assert.equal(mockContext.bindFramebuffer.args[0][1], mockInstance.framebuffer); assert.equal(mockInstance.updateMaxTexSize.callCount, 1); assert.equal(mockInstance.framebuffer.width, 100); assert.equal(mockInstance.framebuffer.height, 100); assert.equal(mockContext.viewport.callCount, 1); assert.equal(mockContext.viewport.args[0][0], 0); assert.equal(mockContext.viewport.args[0][1], 0); assert.equal(mockContext.viewport.args[0][2], 123); assert.equal(mockContext.viewport.args[0][3], 321); assert.equal(mockInstance.canvas.width, 123); assert.equal(mockInstance.canvas.height, 321);
const childTemplate = '<custom slot="" name="custom element"></custom>'; transformInstance.applyTransforms('', childTemplate); const slotMap = mockSerializer.args[0][1].slotMap; assert(slotMap.has('default')); }); const childTemplate = '<!-- nice comment -->'; transformInstance.applyTransforms('', childTemplate); const slotMap = mockSerializer.args[0][1].slotMap; assert(slotMap.has('default')); }); `; transformInstance.applyTransforms('', childTemplate); const slotMap = mockSerializer.args[0][1].slotMap; assert.equal(slotMap.size, 3); assert.ok(slotMap.get('default')); `; transformInstance.applyTransforms('', childTemplate); const slotMap = mockSerializer.args[0][1].slotMap; assert.equal(slotMap.size, 2); const options = mockSerializer.args[0][1]; assert(options.slotMap instanceof Map); assert(options.treeAdapter instanceof Object);
mockTemplate.returns(cacheTemplate.args[0][0]);
it('emits `start(request)` event', done => { const onStart = sinon.spy(); nock('https://fragment') .get('/') .reply(200, 'hello'); mockTemplate.returns('<fragment src="https://fragment">'); tailor.on('start', onStart); http.get('http://localhost:8080/template', response => { response.resume(); response.on('end', () => { const request = onStart.args[0][0]; assert.equal(request.url, '/template'); assert.equal(onStart.callCount, 1); done(); }); }); });
it('emits `response(request, statusCode, headers)` event', done => { const onResponse = sinon.spy(); mockTemplate.returns('<html>'); tailor.on('response', onResponse); http.get('http://localhost:8080/template', response => { response.resume(); response.on('end', () => { const request = onResponse.args[0][0]; const statusCode = onResponse.args[0][1]; const headers = onResponse.args[0][2]; assert.equal(request.url, '/template'); assert.equal(statusCode, 200); assert.deepEqual(headers, { 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Content-Type': 'text/html', Pragma: 'no-cache' }); assert.equal(onResponse.callCount, 1); done(); }); }); });
it('emits `end(request, contentSize)` event', done => { const onEnd = sinon.spy(); mockTemplate.returns( '<html><head></head><body><h2></h2></body></html>' ); tailor.on('end', onEnd); http.get('http://localhost:8080/template', response => { response.resume(); response.on('end', () => { const request = onEnd.args[0][0]; const contentSize = onEnd.args[0][1]; assert.equal(request.url, '/template'); assert.equal(contentSize, 48); assert.equal(onEnd.callCount, 1); done(); }); }); });
test('Should remove by url', done => { const rmServer = nock('http://localhost:8080').post(`/remove/${url}`).reply(204); const consoleSpy = sinon.spy(console, 'log'); remove({id: url}).then(() => { // make sure log in was successful // check that server was called expect(rmServer.isDone()).toBeTruthy(); // first check console output expect(consoleSpy.args).toMatchSnapshot(); // restore console console.log.restore(); rmServer.done(); done(); }); });
// test test('Should not deploy with broken config', done => { // spy on console const consoleSpy = sinon.spy(console, 'log'); // corrupt config with string fs.writeFileSync(path.join(__dirname, '..', 'exoframe.json'), 'I am broken json now'); // execute deploy deploy().then(() => { // check console output expect(consoleSpy.args).toMatchSnapshot(); // restore console console.log.restore(); done(); }); });
it('emits `error(request, error)` event on primary error/timeout', done => { const onPrimaryError = sinon.spy(); nock('https://fragment') .get('/') .reply(500); mockTemplate.returns('<fragment primary src="https://fragment">'); tailor.on('error', onPrimaryError); http.get('http://localhost:8080/template', response => { const request = onPrimaryError.args[0][0]; const error = onPrimaryError.args[0][1]; assert.equal(request.url, '/template'); assert.equal(error.message, 'Internal Server Error'); response.resume(); response.on('end', done); }); });
test('Should remove by token instead of default auth', done => { const rmServer = nock('http://localhost:8080').post(`/remove/${id}`).reply(204); const consoleSpy = sinon.spy(console, 'log'); remove({id: id, token: 'test-token'}).then(() => { // make sure log in was successful // check that server was called expect(rmServer.isDone()).toBeTruthy(); // first check console output expect(consoleSpy.args).toMatchSnapshot(); // restore console console.log.restore(); rmServer.done(); done(); }); });