def test_render_amd_includes_spec(self): from pyramid_amdjs.amd import init_amd_spec, ID_AMD_SPEC, RESOLVER self.cfg['amd.enabled'] = True self.cfg['amd.spec-dir'] = RESOLVER.resolve( 'pyramid_amdjs:tests/dir/').abspath() self.cfg['amd.spec'] = [('test', 'test.js')] init_amd_spec(self.config) self.registry[ID_AMD_SPEC] = { 'test': {'test.js': {'path': '/test/test.js'}}, 'test-init': RESOLVER.resolve( 'pyramid_amdjs:tests/dir/test2.js').abspath()} text = self.request.init_amd('test').strip() self.assertIn( '<script src="http://example.com/_amdjs/' 'bundles/test2.js?_v=123"> </script>', text) m_static = self.request.static_url = mock.Mock() m_static.return_value = 'http://example.com/_amd_test/test.js' text = self.request.init_amd('test', 'test').strip() self.assertIn( '<script src="http://example.com/_amd_test/test.js"> </script>\n' '<script src="http://example.com/_amd_test/test.js"></script>', text)
def test_tween_spec(self): self.registry.settings['amd.enabled'] = True self.registry.settings['amd.spec-dir'] = RESOLVER.resolve( 'pyramid_amdjs:tests/dir/').abspath() self.registry.settings['amd.spec'] = [('test', 'test.js')] init_amd_spec(self.config) self.registry[ID_AMD_SPEC] = { 'test': {'test.js': {'path': '/test/test.js'}}, 'test-init': RESOLVER.resolve( 'pyramid_amdjs:tests/dir/test2.js').abspath()} self.js = ['mod1', 'mod2'] self.css = ['css1'] self.fn = [(('jquery',), 'function($){}')] self.spec = 'test' res = self._make_one(self.request).text self.assertIn( '<script src="http://example.com/_amdjs/bundles/test2.js', res) self.assertIn( '<script type="text/javascript">curl({paths:pyramid_amd_modules},' "['mod1','mod2'])</script>", res) self.assertIn( "curl(['jquery'], function($){})", res)
def test_tween_spec(self): self.registry.settings['amd.enabled'] = True self.registry.settings['amd.spec-dir'] = RESOLVER.resolve( 'pyramid_amdjs:tests/dir/').abspath() self.registry.settings['amd.spec'] = [('test', 'test.js')] init_amd_spec(self.config) self.registry[ID_AMD_SPEC] = { 'test': { 'test.js': { 'path': '/test/test.js' } }, 'test-init': RESOLVER.resolve('pyramid_amdjs:tests/dir/test2.js').abspath() } self.js = ['mod1', 'mod2'] self.css = ['css1'] self.fn = [(('jquery', ), 'function($){}')] self.spec = 'test' res = self._make_one(self.request).text self.assertIn( '<script src="http://example.com/_amdjs/bundles/test2.js', res) self.assertIn( '<script type="text/javascript">curl({paths:pyramid_amd_modules},' "['mod1','mod2'])</script>", res) self.assertIn("curl(['jquery'], function($){})", res)
def test_build_md5(self): from pyramid_amdjs.amd import build_md5, ID_AMD_SPEC, RESOLVER f = RESOLVER.resolve('pyramid_amdjs:tests/dir/test2.js').abspath() reg = self.registry reg.settings['amd.enabled'] = True reg[ID_AMD_SPEC]['test'] = {'test-init': f} reg[ID_AMD_SPEC]['test-init'] = f h = build_md5(self.request, 'test') self.assertEqual('123', h)
def test_amd_init_from_file(self): from pyramid_amdjs.amd import amd_init, RESOLVER, ID_AMD_SPEC self.request.params['_v'] = '123' self.request.matchdict['specname'] = 'test' self.registry[ID_AMD_SPEC] = \ {'test': {'pyramid': {}}, 'test-init': RESOLVER.resolve( 'pyramid_amdjs:tests/dir/test2.js').abspath()} resp = amd_init(self.request) self.assertIsInstance(resp, FileResponse) self.assertEqual('max-age=31536000', resp.headers['Cache-Control'])
def test_register_static_view_for_specs(self): from pyramid_amdjs.amd import init_amd_spec, RESOLVER d = RESOLVER.resolve('pyramid_amdjs:tests/dir/').abspath() cfg = self.registry.settings cfg['amd.spec'] = [('test', 'test2.js')] cfg['amd.spec-dir'] = d m_asv = self.config.add_static_view = mock.Mock() init_amd_spec(self.config) self.assertTrue(m_asv.called) name, path = m_asv.call_args[0] self.assertEqual('_amdjs/bundles/', name) self.assertTrue(path.endswith('pyramid_amdjs/tests/dir'))