-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
206 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* eslint-env jest */ | ||
|
||
const fs = require('fs').promises | ||
const path = require('path') | ||
|
||
const updateUtils = require('./update-utils') | ||
updateUtils.getProjectVersion = jest.fn() | ||
updateUtils.fetchOriginal = jest.fn() | ||
|
||
const updatejs = require('./_update_javascripts') | ||
|
||
const oldApplicationJs = `/* global $ */ | ||
// Warn about using the kit in production | ||
if (window.console && window.console.info) { | ||
window.console.info('GOV.UK Prototype Kit - do not use for production') | ||
} | ||
$(document).ready(function () { | ||
window.GOVUKFrontend.initAll() | ||
}) | ||
` | ||
|
||
const newApplicationJs = `// Add extra JavaScript here | ||
ready(() => { | ||
// Add JavaScript that needs to be run when the page is loaded | ||
}) | ||
` | ||
|
||
describe('removeKitJsFromApplicationJs', () => { | ||
afterEach(() => { | ||
jest.restoreAllMocks() | ||
}) | ||
|
||
beforeEach(() => { | ||
jest.spyOn(updateUtils, 'getProjectVersion').mockImplementation( | ||
async () => '12.1.1' | ||
) | ||
|
||
jest.spyOn(updateUtils, 'fetchOriginal').mockImplementation( | ||
async () => oldApplicationJs | ||
) | ||
}) | ||
|
||
it('replaces application.js if the user has not updated it', async () => { | ||
jest.spyOn(fs, 'readFile').mockImplementationOnce( | ||
async () => oldApplicationJs | ||
) | ||
|
||
const mockCopyFile = jest.spyOn(fs, 'copyFile').mockImplementation( | ||
async () => {} | ||
) | ||
|
||
await updatejs.removeKitJsFromApplicationJs() | ||
|
||
expect(mockCopyFile).toHaveBeenCalledWith( | ||
expect.stringContaining(path.join('update', 'app', 'assets', 'javascripts', 'application.js')), | ||
expect.stringContaining(path.join('app', 'assets', 'javascripts', 'application.js')) | ||
) | ||
}) | ||
|
||
it('rewrites application.js if the user has added lines to the bottom of the file', async () => { | ||
// theirs | ||
jest.spyOn(fs, 'readFile').mockImplementationOnce( | ||
async () => oldApplicationJs + '\ncallMyCode()\n' | ||
) | ||
// ours | ||
jest.spyOn(fs, 'readFile').mockImplementationOnce( | ||
async () => newApplicationJs | ||
) | ||
|
||
const mockWriteFile = jest.spyOn(fs, 'writeFile').mockImplementation( | ||
async () => {} | ||
) | ||
|
||
await updatejs.removeKitJsFromApplicationJs() | ||
|
||
expect(mockWriteFile).toHaveBeenCalledWith( | ||
expect.stringContaining(path.join('app', 'assets', 'javascripts', 'application.js')), | ||
newApplicationJs + '\ncallMyCode()\n', | ||
'utf8' | ||
|
||
it('does not touch application.js if the user has rewritten it a lot', async () => { | ||
// theirs | ||
jest.spyOn(fs, 'readFile').mockImplementationOnce( | ||
async () => 'justMyCode()\n' | ||
) | ||
|
||
const mockWriteFile = jest.spyOn(fs, 'writeFile').mockImplementation( | ||
async () => {} | ||
) | ||
|
||
const mockCopyFile = jest.spyOn(fs, 'copyFile').mockImplementation( | ||
async () => {} | ||
) | ||
|
||
await updatejs.removeKitJsFromApplicationJs() | ||
|
||
expect(mockWriteFile).not.toHaveBeenCalled() | ||
expect(mockCopyFile).not.toHaveBeenCalled() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const fs = require('fs').promises | ||
const https = require('https') | ||
const path = require('path') | ||
|
||
const { projectDir } = require('./path-utils') | ||
|
||
async function getProjectVersion () { | ||
return (await fs.readFile(path.join(projectDir, 'VERSION.txt'), 'utf8')).trim() | ||
} | ||
|
||
async function fetchOriginal (version, filePath) { | ||
const remoteUrl = `https://raw.githubusercontent.com/alphagov/govuk-prototype-kit/v${version}/${filePath}` | ||
|
||
let data = '' | ||
return new Promise((resolve, reject) => { | ||
https.get(remoteUrl, (response) => { | ||
let error | ||
|
||
if (response.statusCode !== 200) { | ||
error = new Error(`could not fetch ${remoteUrl}: status code ${response.statusCode}`) | ||
Object.assign(error, response) | ||
response.resume() | ||
reject(error) | ||
} | ||
|
||
response.setEncoding('utf8') | ||
|
||
response.on('data', (chunk) => { | ||
data += chunk | ||
}) | ||
|
||
response.on('end', () => { | ||
resolve(data) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
module.exports = { | ||
fetchOriginal, | ||
getProjectVersion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* eslint-env jest */ | ||
|
||
const fs = require('fs').promises | ||
const https = require('https') | ||
const stream = require('stream') | ||
|
||
const { fetchOriginal, getProjectVersion } = require('./update-utils.js') | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks() | ||
}) | ||
|
||
describe('fetchOriginal', () => { | ||
it('gets the contents of a file as of version from GitHub', async () => { | ||
const mockHttpsGet = jest.spyOn(https, 'get').mockImplementation((url, callback) => { | ||
const response = new stream.PassThrough() | ||
response.statusCode = 200 | ||
|
||
callback(response) | ||
|
||
response.write('foo\n') | ||
response.write('bar\n') | ||
response.end() | ||
}) | ||
|
||
await expect(fetchOriginal('99.99.99', 'app/views/foo.html')).resolves.toEqual( | ||
'foo\nbar\n' | ||
) | ||
expect(mockHttpsGet).toHaveBeenCalledWith( | ||
'https://raw.githubusercontent.com/alphagov/govuk-prototype-kit/v99.99.99/app/views/foo.html', | ||
expect.anything() | ||
) | ||
}) | ||
}) | ||
|
||
describe('getProjectVersion', () => { | ||
it('reads the VERSION.txt file to get the version number', async () => { | ||
const mockReadFile = jest.spyOn(fs, 'readFile').mockImplementation( | ||
async () => '99.99.99\n' | ||
) | ||
|
||
await expect(getProjectVersion()).resolves.toEqual('99.99.99') | ||
expect(mockReadFile).toHaveBeenCalledWith( | ||
expect.stringMatching(/.*\/VERSION.txt$/), | ||
'utf8' | ||
) | ||
}) | ||
}) |