From c9653a5bce2b3e092f5662b4eb87dbcc9d70c57f Mon Sep 17 00:00:00 2001 From: Wilco Fiers Date: Thu, 7 Mar 2019 19:01:15 +0100 Subject: [PATCH] fix: Avoid "screen is not defined" error [#1404] (#1415) * fix: Avoid "screen is not defined" error (#1404) * add comments for temp solution --- .../reporters/helpers/get-environment-data.js | 25 ++++++---- .../reporters/helpers/get-environment-data.js | 50 +++++++++++++++++++ 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/lib/core/reporters/helpers/get-environment-data.js b/lib/core/reporters/helpers/get-environment-data.js index 6284a5cc25..dcce379d60 100644 --- a/lib/core/reporters/helpers/get-environment-data.js +++ b/lib/core/reporters/helpers/get-environment-data.js @@ -4,12 +4,19 @@ * Add information about the environment axe was run in. * @return {Object} */ -helpers.getEnvironmentData = function getEnvironmentData() { - 'use strict'; - var orientation = window.screen - ? screen.msOrientation || - (screen.orientation || screen.mozOrientation || {}) - : {}; +helpers.getEnvironmentData = function getEnvironmentData(win = window) { + // TODO: remove parameter once we are testing axe-core in jsdom and other + // supported environments + const { + screen = {}, + navigator = {}, + location = {}, + innerHeight, + innerWidth + } = win; + + const orientation = + screen.msOrientation || screen.orientation || screen.mozOrientation || {}; return { testEngine: { @@ -21,12 +28,12 @@ helpers.getEnvironmentData = function getEnvironmentData() { }, testEnvironment: { userAgent: navigator.userAgent, - windowWidth: window.innerWidth, - windowHeight: window.innerHeight, + windowWidth: innerWidth, + windowHeight: innerHeight, orientationAngle: orientation.angle, orientationType: orientation.type }, timestamp: new Date().toISOString(), - url: window.location.href + url: location.href }; }; diff --git a/test/core/reporters/helpers/get-environment-data.js b/test/core/reporters/helpers/get-environment-data.js index 315390c469..16f2b7b8be 100644 --- a/test/core/reporters/helpers/get-environment-data.js +++ b/test/core/reporters/helpers/get-environment-data.js @@ -1,5 +1,14 @@ describe('helpers.getEnvironmentData', function() { 'use strict'; + var __audit; + before(function() { + __audit = axe._audit; + axe._audit = { brand: 'Deque' }; + }); + + after(function() { + axe._audit = __audit; + }); it('should return a `testEngine` property', function() { var data = helpers.getEnvironmentData(); @@ -33,4 +42,45 @@ describe('helpers.getEnvironmentData', function() { var data = helpers.getEnvironmentData(); assert.isDefined(data.url); }); + + // TODO: remove or update test once we are testing axe-core in jsdom and + // other supported environments as what this is testing should be done in + // those environment tests + it('gets data from the `win` parameter when passed', function() { + var data = helpers.getEnvironmentData({ + screen: { + orientation: { + type: 'fictional', + angle: 'slanted' + } + }, + navigator: { + userAgent: 'foo' + }, + location: { + href: 'foo://' + }, + innerWidth: 321, + innerHeight: 123 + }); + + delete data.timestamp; + assert.deepEqual(data, { + testEngine: { + name: 'axe-core', + version: axe.version + }, + testRunner: { + name: axe._audit.brand + }, + testEnvironment: { + userAgent: 'foo', + windowWidth: 321, + windowHeight: 123, + orientationAngle: 'slanted', + orientationType: 'fictional' + }, + url: 'foo://' + }); + }); });