2014-06-24 6 views
5

저는 테스트 프레임 워크를 위해 Mocha/Chai/Karma를 연결하려고합니다. Polymer unit 테스트 가이드를 기본으로 거의 사용했습니다. 훨씬 간단한 것을 원했고 단지 tests.js 스크립트 만 필요하고 bower 구성 요소 vca-tests을 사용했습니다. 이는`tests.js 보이는 방법입니다 <iframe>에 저장소의 경로에서 구성 요소에수동으로 폴리머 엘리먼트를로드하는 방법

<!doctype html> 
<html> 
    <head> 
    <title>VCA Element Test Runner</title> 
    <meta charset="UTF-8"> 

    <!-- Load in the frameworks we need --> 
    <script src="../platform/platform.js"></script> 
    <link rel="import" href="../polymer/polymer.html"> 
    <link rel="import" href="vca-tests.html"> 
    </head> 
    <body> 
    <div id="mocha"></div> 
    </body> 
</html> 

elementSuite 기능로드 :

(function() { 
    'user strict'; 
    elementSuite('vca-colour-picker', function() { 
    test('setting the web value should be reflected in the RGB values', function(done) { 
     this.set(function(element) { 
     element.web = '#FFCC88'; 
     }).then(function(element) { 
     assert.equal(element.r, 255); 
     assert.equal(element.g, 204); 
     assert.equal(element.b, 136); 
     }); 
    }); 
    }); 
}()); 

그래서 나는 모카 테스트 러너를 썼다. 방법으로 인해 bower 작품의 수입 위치를 수정해야합니다. bower_components 폴더 내부에서로드의 vca-test.html는 :

<!-- Test frameworks --> 
<link rel="stylesheet" href="../mocha/mocha.css" /> 
<script src="../mocha/mocha.js"></script> 
<script src="../chai/chai.js"></script> 

<!-- Perform some set up --> 
<script> 
(function() { 
    'use strict'; 
    var iframe, 
     documents = {}; 

    function elementImport(suite, name, done) { 
    if (this.status === 200) { 
     var i, 
      doc = iframe.contentDocument, 
      link = doc.createElement('link'), 
      win = iframe.contentWindow, 
      head = doc.getElementsByTagName('head')[0], 
      body = doc.body, 
      origin = window.location.origin, 
      links = this.response.querySelectorAll('link[rel="import"]'), 
      element = this.response.querySelector('polymer-element'); 

     // Fix up the import paths as they will be referencing the root path and we 
     // need the relative to the bower components (which is where we are now) 
     for (i = 0; i < links.length; ++i) { 
     links[i].href = links[i].href.replace(origin, '..'); 
     doc.adoptNode(links[i]); 
     head.appendChild(links[i]); 
     } 

     // Make sure polymer is fired up 
     doc.addEventListener('polymer-ready', function() { 

// -------------------------------------------------------------------------- 
// At this point we have loaded all of the dependent components of the 
// component to test but we still need to be able to create instances 
// of the component. The below code will work, but because it is loaded 
// to the root of the repository all the dependencies will fail with 404s 

     /*/ Import the rest of the component 
     link.rel = 'import'; 
     link.href = '../../' + name + '.html'; 
     link.import = this.response; 
     head.appendChild(link);/**/ 

// -------------------------------------------------------------------------- 
     // Create the element 
     suite.ctx.element = doc.createElement(name); 
     body.appendChild(suite.ctx.element); 

     // Tell mocha that we are done 
     done(); 
     }, false); 
    } 
    } 

    function elementLoad(name, done) { 
    var xhr = new XMLHttpRequest(), 
     path = '../../' + name + '.html'; 
    xhr.open('GET', path, true); 
    xhr.responseType = 'document'; 
    xhr.onload = elementImport.bind(xhr, this, name, done); 
    xhr.send(); 
    } 

    function elementSetup(name, done) { 
    iframe = document.createElement('iframe'); 
    iframe.style.cssText = /*'position: absolute;left:-10000em;*/'width:768px;height:1024px'; 
    iframe.onload = elementLoad.bind(this, name, done); 
    iframe.src = 'iframe.html'; 
    document.body.appendChild(iframe); 

    // Provide convience functions 
    this.ctx.set = function(callback) { 
     callback.call(this, this.element); 
     Platform.flush(); 
     return this; 
    }; 
    this.ctx.then = function(callback) { 
     setTimeout(function() { 
     callback.call(this, this.element); 
     this.test.callback(); 
     }.bind(this), 50); 
     return this; 
    }; 
    } 

    function elementTeardown() { 
// document.body.removeChild(iframe); 
    } 

    // This is what the tests.js script uses to register an element to test 
    window.elementSuite = function(name, tests) { 
    suite(name, function() { 
     setup(elementSetup.bind(this, name)); 
     teardown(elementTeardown); 
     tests(); 
    }); 
    } 

    // We use chai as our assertion framework 
    window.assert = chai.assert; 

    // Mocha runs our tests 
    mocha.setup({ui: 'tdd', slow: 1000, timeout: 5000}); 
}()); 
</script> 

<!-- Load in the test script --> 
<script src="../../tests.js"></script> 

<!-- Run the tests --> 
<script> 
mocha.run(); 
</script> 

그래서 내 질문이 아래로 비등 : 가 어떻게이 polymer-element를 포함하는 HTML 문서를 XHR 수 있지만 그래서 내 상대 이물 구성 요소로드를 기본 href을 변경?

폴리머는 <link rel="import">이로드되면 해당 요소를 사용할 수있는 것처럼 보입니다.

+0

Polymer 코드의 내부를 보면'href '로'data :'URI를 사용하지만 Polymer HTMLImports polyfill은 지원하지 않는 것 같습니다. 패치를보고. –

답변

2

편집 : HTML 가져 오기는 사양에 data:text/html을 지원하지 않으므로 polyfill을 사용할 때는 만 사용할 수 있습니다.

// Do a local import 
link.rel = 'import'; 
link.href = 'data:text/html,' + encodeURIComponent(component); 

// Monkey Patch Polymer, to fake the location of the 'import' 
if (iframe.contentWindow.HTMLImports && !iframe.contentWindow.HTMLImports.useNative) { 
    var hi = iframe.contentWindow.HTMLImports, 
     importLoader = hi.importLoader, 
     receive = importLoader.receive, 
     origin = iframe.src.substr(0, iframe.src.lastIndexOf('/', iframe.src.lastIndexOf('/') - 1)); 
    importLoader.receive = function(args) { 
    if (arguments[0] === link.href) { 
     var i, redirected = []; 
     for (i = 0; i < arguments.length; ++i) { 
     redirected.push(arguments[i]); 
     } 
     redirected.push(origin + '/' + name + '/' + name + '.html'); 
     receive.apply(this, redirected); 
    } else { 
     receive.apply(this, arguments); 
    } 
    } 
} 

// Add the link now that we are monkey patched 
doc.head.appendChild(link); 

HTMLImports이 글을 쓰는 시점에서 제대로 데이터 URI를 처리하지 않습니다

끔찍한 원숭이 패치를 사용했다. 현재 프로젝트의 패치를 작성하는 방법을 모릅니다.

+0

간편한 재사용을 위해 Karma 플러그인에 이것을 구현했습니다 : https://www.npmjs.org/package/karma-polymer-test –

관련 문제