Skip to content

Commit

Permalink
Tests: Added getResourceTiming() tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicjansma committed Nov 26, 2018
1 parent 720e0e6 commit 8142721
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
57 changes: 56 additions & 1 deletion test/test-resourcetiming-compression.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env node, mocha */
/* eslint-disable max-len */
/* eslint-disable max-len, no-unused-expressions */
(function(root) {
"use strict";

Expand All @@ -14,6 +14,10 @@
root.ResourceTimingCompression :
require("../src/resourcetiming-compression");

var ResourceTimingDecompression = root.ResourceTimingDecompression ?
root.ResourceTimingDecompression :
require("../src/resourcetiming-decompression");

var expect = root.expect ? root.expect : require("expect.js");

//
Expand Down Expand Up @@ -752,5 +756,56 @@
});
});
});

//
// .compressResourceTiming
//
describe(".getResourceTiming()", function() {
it("Should get an object with .restiming and .servertiming from the page", function() {
var results = ResourceTimingCompression.getResourceTiming();

expect(results).to.have.own.property("restiming");
expect(results).to.have.own.property("servertiming");
});

it("Should contain the scaled PNG image", function() {
var results = ResourceTimingCompression.getResourceTiming();
var resources = ResourceTimingDecompression.decompressResources(results.restiming, results.servertiming);
var scaledImage = resources.find(function(r) {
return r.name.indexOf("?scaled") !== -1;
});

expect(scaledImage).to.exist;
});

it("Should contain the scaled PNG image with timings set", function() {
var results = ResourceTimingCompression.getResourceTiming();
var resources = ResourceTimingDecompression.decompressResources(results.restiming, results.servertiming);
var scaledImage = resources.find(function(r) {
return r.name.indexOf("?scaled") !== -1;
});

expect(scaledImage).to.have.own.property("startTime");
expect(scaledImage.startTime).to.be.above(0);

expect(scaledImage).to.have.own.property("responseEnd");
expect(scaledImage.responseEnd).to.be.above(0);
});

it("Should contain the scaled PNG image with dimensions set", function() {
var results = ResourceTimingCompression.getResourceTiming();
var resources = ResourceTimingDecompression.decompressResources(results.restiming, results.servertiming);
var scaledImage = resources.find(function(r) {
return r.name.indexOf("?scaled") !== -1;
});

expect(scaledImage.height).to.equal(100);
expect(scaledImage.width).to.equal(100);
expect(scaledImage.x).to.be.above(100);
expect(scaledImage.y).to.be.above(100);
expect(scaledImage.naturalHeight).to.equal(1000);
expect(scaledImage.naturalWidth).to.equal(1000);
});
});
});
}(typeof window !== "undefined" ? window : undefined));
8 changes: 7 additions & 1 deletion test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@
</head>
<body>
<div id="mocha"></div>
<img src="test.png?natural" />
<img src="test.png?scaled" height="100" width="100" />
<script>
var img = new Image();
img.src = "test.png?script";
</script>
<script>
mocha.setup('bdd');
</script>
<script src="test-resourcetiming-compression.js"></script>
<script src="test-resourcetiming-decompression.js"></script>
<script>
mocha.run();
window.addEventListener("load", mocha.run);
</script>
</body>
</html>
Binary file added test/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8142721

Please sign in to comment.