Skip to content

Commit

Permalink
Merge pull request #181 from alphagov/add-linter
Browse files Browse the repository at this point in the history
Add linter to govuk browser extension
  • Loading branch information
kashifatcha authored Jan 26, 2024
2 parents bd11e79 + 5f26cbd commit 2bd5e5d
Show file tree
Hide file tree
Showing 26 changed files with 3,773 additions and 1,149 deletions.
3,177 changes: 2,897 additions & 280 deletions package-lock.json

Large diffs are not rendered by default.

25 changes: 24 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
{
"devDependencies": {
"jasmine-browser-runner": "^2.3.0",
"jasmine-core": "^5.1.1"
"jasmine-core": "^5.1.1",
"standardx": "^7.0.0"
},
"scripts": {
"lint:js": "standardx 'spec/javascripts/**/*.js' 'src/**/*.js'",
"lint:js:fix": "npm run lint:js --fix",
"test": "jasmine-browser-runner runSpecs",
"build": "bash build.sh"
},
"eslintConfig": {
"env": {
"browser": true,
"jasmine": true
},
"rules":{
"no-var": 0,
"no-unused-vars": 0,
"no-use-before-define": 0
}
},
"standardx": {
"global": [
"chrome",
"Mustache",
"fetch",
"Popup",
"pluck"
]
}
}
4 changes: 2 additions & 2 deletions spec/helpers/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function pluck(array, key) {
function pluck (array, key) {
return array.map(function (object) {
return object[key];
return object[key]
})
}
6 changes: 3 additions & 3 deletions spec/helpers/polyfills/String.startsWith.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position){
return this.substr(position || 0, searchString.length) === searchString;
};
String.prototype.startsWith = function (searchString, position) {
return this.substr(position || 0, searchString.length) === searchString
}
}
98 changes: 49 additions & 49 deletions spec/javascripts/ab_tests.spec.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,73 @@
describe("Popup.findActiveAbTests", function () {
it("returns no A/B tests if none are active", function () {
var abTests = Popup.findActiveAbTests({});
describe('Popup.findActiveAbTests', function () {
it('returns no A/B tests if none are active', function () {
var abTests = Popup.findActiveAbTests({})

expect(abTests).toEqual([]);
});
expect(abTests).toEqual([])
})

it("finds all A/B tests", function () {
it('finds all A/B tests', function () {
var abTests = Popup.findActiveAbTests({
"first-AB-test-name": {
currentBucket: "some-value",
allowedBuckets: ["some-value", "B"]
'first-AB-test-name': {
currentBucket: 'some-value',
allowedBuckets: ['some-value', 'B']
},
"second-AB-test-name": {
currentBucket: "other-value",
allowedBuckets: ["other-value", "B"]
'second-AB-test-name': {
currentBucket: 'other-value',
allowedBuckets: ['other-value', 'B']
},
"third-AB-test-name": {
currentBucket: "yet-another-value",
allowedBuckets: ["A", "yet-another-value"]
'third-AB-test-name': {
currentBucket: 'yet-another-value',
allowedBuckets: ['A', 'yet-another-value']
}
});
})

expect(abTests.length).toEqual(3);
expect(abTests[0].testName).toEqual("first-AB-test-name");
expect(abTests[1].testName).toEqual("second-AB-test-name");
expect(abTests[2].testName).toEqual("third-AB-test-name");
});
expect(abTests.length).toEqual(3)
expect(abTests[0].testName).toEqual('first-AB-test-name')
expect(abTests[1].testName).toEqual('second-AB-test-name')
expect(abTests[2].testName).toEqual('third-AB-test-name')
})

it("returns A and B buckets", function () {
it('returns A and B buckets', function () {
var abTests = Popup.findActiveAbTests({
"some-AB-test-name": {
currentBucket: "A",
allowedBuckets: ["A", "B"]
'some-AB-test-name': {
currentBucket: 'A',
allowedBuckets: ['A', 'B']
}
});
})

expect(abTests[0].buckets.length).toEqual(2);
expect(abTests[0].buckets[0].bucketName).toEqual("A");
expect(abTests[0].buckets[1].bucketName).toEqual("B");
expect(abTests[0].buckets.length).toEqual(2)
expect(abTests[0].buckets[0].bucketName).toEqual('A')
expect(abTests[0].buckets[1].bucketName).toEqual('B')
})

it("highlights 'A' bucket if user is in 'A' group", function () {
var abTests = Popup.findActiveAbTests({
"some-AB-test-name": {
currentBucket: "B",
allowedBuckets: ["A", "B"]
'some-AB-test-name': {
currentBucket: 'B',
allowedBuckets: ['A', 'B']
},
"other-AB-test-name": {
currentBucket: "A",
allowedBuckets: ["A", "B"]
'other-AB-test-name': {
currentBucket: 'A',
allowedBuckets: ['A', 'B']
}
});
})

expect(abTests[0].buckets[0].class).toEqual("");
expect(abTests[0].buckets[1].class).toEqual("ab-bucket-selected");
expect(abTests[0].buckets[0].class).toEqual('')
expect(abTests[0].buckets[1].class).toEqual('ab-bucket-selected')

expect(abTests[1].buckets[0].class).toEqual("ab-bucket-selected");
expect(abTests[1].buckets[1].class).toEqual("");
});
expect(abTests[1].buckets[0].class).toEqual('ab-bucket-selected')
expect(abTests[1].buckets[1].class).toEqual('')
})

it("doesn't highlight any buckets if variant is unknown", function () {
var abTests = Popup.findActiveAbTests({
"some-AB-test-name": {
currentBucket: "Unknown",
allowedBuckets: ["A", "B"]
'some-AB-test-name': {
currentBucket: 'Unknown',
allowedBuckets: ['A', 'B']
}
});
})

expect(abTests[0].buckets[0].class).toEqual("");
expect(abTests[0].buckets[1].class).toEqual("");
});
});
expect(abTests[0].buckets[0].class).toEqual('')
expect(abTests[0].buckets[1].class).toEqual('')
})
})
64 changes: 32 additions & 32 deletions spec/javascripts/content_links.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
describe("PopupView.generateContentLinks", function () {
describe('PopupView.generateContentLinks', function () {
var PROD_ENV = { protocol: 'https', serviceDomain: 'publishing.service.gov.uk', origin: 'https://www.gov.uk' }
var DRAFT_PROD_ENV = { protocol: 'https', serviceDomain: 'publishing.service.gov.uk', origin: 'https://draft-origin.publishing.service.gov.uk' }

it("returns the correct URIs", function () {
it('returns the correct URIs', function () {
var links = Popup.generateContentLinks(
"https://www.gov.uk/browse/disabilities?foo=bar",
"https://www.gov.uk",
"/browse/disabilities",
'https://www.gov.uk/browse/disabilities?foo=bar',
'https://www.gov.uk',
'/browse/disabilities',
PROD_ENV
)

Expand All @@ -22,16 +22,16 @@ describe("PopupView.generateContentLinks", function () {
'http://webarchive.nationalarchives.gov.uk/*/https://www.gov.uk/browse/disabilities',
'https://content-data.publishing.service.gov.uk/metrics/browse/disabilities',
'https://my2.siteimprove.com/QualityAssurance/1054012/Overview/Search?SearchIn=Url&Query=/browse/disabilities',
'https://search.google.com/structured-data/testing-tool/u/0/#url=https://www.gov.uk/browse/disabilities',
'https://search.google.com/structured-data/testing-tool/u/0/#url=https://www.gov.uk/browse/disabilities'
])
})

it("returns the draft URIs for non-prod environments", function () {
it('returns the draft URIs for non-prod environments', function () {
var links = Popup.generateContentLinks(
"https://www.gov.uk/browse/disabilities?foo=bar",
"https://www.gov.uk",
"/browse/disabilities",
{ protocol: 'https', serviceDomain: 'staging.publishing.service.gov.uk'}
'https://www.gov.uk/browse/disabilities?foo=bar',
'https://www.gov.uk',
'/browse/disabilities',
{ protocol: 'https', serviceDomain: 'staging.publishing.service.gov.uk' }
)

var urls = pluck(links, 'url')
Expand All @@ -41,11 +41,11 @@ describe("PopupView.generateContentLinks", function () {
)
})

it("does not generate URIs for publishing apps (non-www pages)", function () {
it('does not generate URIs for publishing apps (non-www pages)', function () {
var links = Popup.generateContentLinks(
"https://search-admin.publishing.service.gov.uk/queries",
"https://search-admin.publishing.service.gov.uk",
"/queries",
'https://search-admin.publishing.service.gov.uk/queries',
'https://search-admin.publishing.service.gov.uk',
'/queries',
PROD_ENV
)

Expand All @@ -54,9 +54,9 @@ describe("PopupView.generateContentLinks", function () {

it("only generates URLs for publishing-apps when it's the support application", function () {
var links = Popup.generateContentLinks(
"https://support.publishing.service.gov.uk/anonymous_feedback?path=/browse/disabilities",
"https://support.publishing.service.gov.uk",
"/anonymous_feedback",
'https://support.publishing.service.gov.uk/anonymous_feedback?path=/browse/disabilities',
'https://support.publishing.service.gov.uk',
'/anonymous_feedback',
PROD_ENV
)

Expand All @@ -76,33 +76,33 @@ describe("PopupView.generateContentLinks", function () {
])
})

it("generates a link for smart answers", function () {
it('generates a link for smart answers', function () {
var links = Popup.generateContentLinks(
"https://www.gov.uk/smart-answer/y/question-1",
"https://www.gov.uk",
"/smart-answer/y/question-1",
'https://www.gov.uk/smart-answer/y/question-1',
'https://www.gov.uk',
'/smart-answer/y/question-1',
PROD_ENV,
"smartanswers"
'smartanswers'
)

var urls = pluck(links, 'url')

expect(urls).toContain("https://www.gov.uk/smart-answer/y/visualise")
expect(urls).toContain('https://www.gov.uk/smart-answer/y/visualise')
})

it("generates correct link for content API on draft stack", function () {
it('generates correct link for content API on draft stack', function () {
var links = Popup.generateContentLinks(
"https://draft-origin.publishing.service.gov.uk/apply-for-and-manage-a-gov-uk-domain-name",
"https://draft-origin.publishing.service.gov.uk",
"/apply-for-and-manage-a-gov-uk-domain-name",
'https://draft-origin.publishing.service.gov.uk/apply-for-and-manage-a-gov-uk-domain-name',
'https://draft-origin.publishing.service.gov.uk',
'/apply-for-and-manage-a-gov-uk-domain-name',
DRAFT_PROD_ENV,
"collections"
'collections'
)

expect(links[1]).toEqual({
name: "Content item (JSON)",
url: "https://draft-origin.publishing.service.gov.uk/api/content/apply-for-and-manage-a-gov-uk-domain-name",
class: ""
name: 'Content item (JSON)',
url: 'https://draft-origin.publishing.service.gov.uk/api/content/apply-for-and-manage-a-gov-uk-domain-name',
class: ''
})
})
})
53 changes: 28 additions & 25 deletions spec/javascripts/design_mode_component.spec.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
"use strict";
describe("Toggling design mode", function () {
var designModeBannerId = "govuk-chrome-toolkit-design-mode-banner";
var designModeBannerElement;
var designModeComponent;
'use strict'
/* global DesignModeComponent */

describe('Toggling design mode', function () {
var designModeBannerId = 'govuk-chrome-toolkit-design-mode-banner'
var designModeBannerElement
var designModeComponent

beforeEach(function () {
// Mock addListener function to call toggleDesignMode trigger when initialized
window.chrome = {
runtime: {
onMessage: {
addListener: function(callback) {
addListener: function (callback) {
/* eslint-disable-next-line */
callback({ trigger: 'toggleDesignMode' })
}
},
sendMessage: function(){}
sendMessage: function () {}
}
};
designModeComponent = new DesignModeComponent;
}
designModeComponent = new DesignModeComponent()
designModeBannerElement = document.querySelector(`#${designModeBannerId}`)
});
})

it("shows design mode banner", function () {
expect(designModeBannerElement.textContent).toMatch(/You are in design mode./);
});
it('shows design mode banner', function () {
expect(designModeBannerElement.textContent).toMatch(/You are in design mode./)
})

it("removes the banner when toggled off", function () {
designModeComponent.toggleDesignMode();
expect(designModeBannerElement).not.toBeVisible();
});
it('removes the banner when toggled off', function () {
designModeComponent.toggleDesignMode()
expect(designModeBannerElement).not.toBeVisible()
})

it("design mode is on when toggled on", function () {
expect(window.document.designMode).toEqual("on");
});
it('design mode is on when toggled on', function () {
expect(window.document.designMode).toEqual('on')
})

it("design mode is off when toggled off", function () {
designModeComponent.toggleDesignMode();
expect(window.document.designMode).toEqual("off");
});
});
it('design mode is off when toggled off', function () {
designModeComponent.toggleDesignMode()
expect(window.document.designMode).toEqual('off')
})
})
Loading

0 comments on commit 2bd5e5d

Please sign in to comment.