<!doctype html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>{{feature}} - Mocha test suite for polyfill-service</title>


	<!-- Test and expectation libraries -->
	<link rel="stylesheet" href="/test/libs/mocha/mocha.css" />
	<script src="/test/libs/mocha/mocha.js"></script>
	<script src="/test/libs/proclaim/proclaim.js"></script>

	<!-- Initalise Mocha -->
	<script>mocha.setup('bdd')</script>

	{{#loadPolyfill}}
		<!-- Code under test -->
		<script src='/v2/polyfill.js?flags=gated{{#forceAlways}},always{{/forceAlways}}&amp;features={{#features}}{{feature}},{{/features}}&amp;rum=1'></script>
	{{/loadPolyfill}}

	<!-- Tests -->
	<script>
	{{#features}}
	describe('{{feature}}', function() {
		it('is running the test framework', function() {
			proclaim.ok(true);
		});
		{{#detect}}
		it('passes a feature detect', function() {
			proclaim.ok((function() {
				return ({{{.}}});
			}).call(window));
		});
		{{/detect}}
		{{#tests}}
			{{{.}}}
		{{/tests}}
	});
	{{/features}}
	</script>
</head>
<body>

	<div id="mocha"></div>

	<script>

	// Given a test, get the first level suite that it is contained within
	// Not the top level, the first one down.
	function getFirstLevelSuite(test) {
		var parent = test;

		while (parent && parent.parent && parent.parent.parent) {
			parent = parent.parent;
		}

		return parent.title;
	}

	// During the test run, surface the test results in Sauce Labs' preferred format
	function run() {
		var runner = mocha.run();
		var results = {
			state: 'complete',
			passed: 0,
			failed: 0,
			total: 0,
			duration: 0,
			tests: [],
			failingSuites: {},
			testedSuites: [],
			uaString: window.navigator.userAgent || 'unknown'
		};
		runner.on('pass', function(test) {
			results.passed++;
			results.total++;
		});

		runner.on('fail', function(test, err) {

			// Get a set of all the suites with failing tests in them.
			if (test.parent) {
				results.failingSuites[getFirstLevelSuite(test)] = true;
			}

			results.failed++;
			results.total++;
			results.tests.push({
				name: test.fullTitle(),
				result: false,
				message: err.message,
				stack: err.stack,
				failingSuite: getFirstLevelSuite(test)
			});
		});

		runner.on('suite', function(suite) {
			results.testedSuites.push(getFirstLevelSuite(suite));
		});

		runner.on('end', function() {

			// Discount one for the 'running the test suite' test
			results.passed--;

			window.global_test_results = results;
			if (parent && parent.receiveTestResults) {
				var flist = "{{#features}}{{feature}},{{/features}}".split(",").slice(0, -1);
				parent.receiveTestResults(flist, results);
			}
		});
	}

	window.onload = run;

	</script>
</body>
</html>
