Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/staging' into feat/issuesTaskLis…
Browse files Browse the repository at this point in the history
…t/routerAndController
  • Loading branch information
GeorgeGoodall committed Aug 6, 2024
2 parents ecd8ee6 + 04122a1 commit e254e64
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 85 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/featureDeploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ jobs:
strategy:
matrix:
environment: ${{ fromJSON(needs.detect-environments.outputs.environments) }}
if: ${{ matrix.environment != 'production' }}
if: ${{ inputs.environment != 'production' }}
uses: ./.github/workflows/deploy.yml
with:
environment: '${{ matrix.environment }}'
environment: '${{ inputs.environment }}'
secrets: inherit


4 changes: 3 additions & 1 deletion src/assets/js/list-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
/* eslint-disable no-var */
//= require govuk_publishing_components/vendor/polyfills/closest

const keyPauseTime = 20

window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {};

Expand Down Expand Up @@ -34,7 +36,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
clearTimeout(this.filterTimeout)
this.filterTimeout = setTimeout(function () {
this.$module.filterList(searchTerm)
}.bind(this), 200)
}.bind(this), keyPauseTime)
}.bind(this))
}

Expand Down
104 changes: 28 additions & 76 deletions src/controllers/OrganisationsController.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datasette from '../services/datasette.js'
import performanceDbApi from '../services/performanceDbApi.js' // Assume you have an API service module
import logger from '../utils/logger.js'
import { dataSubjects } from '../utils/utils.js'
Expand Down Expand Up @@ -70,89 +71,40 @@ const organisationsController = {
}
},

/**
* Handles the GET /organisations request
*
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
async getOrganisations (req, res, next) {
const alphabetisedOrgs = {
A: [
{
name: 'Aberdeen'
},
{
name: 'Aylesbury'
},
{
name: 'Ashford'
}
],
B: [
{
name: 'Bath'
},
{
name: 'Birmingham'
},
{
name: 'Brighton'
}
],
C: [
{
name: 'Cambridge'
},
{
name: 'Cardiff'
},
{
name: 'Cheltenham'
},
{
name: 'Chester'
}
],
D: [
{
name: 'Derby'
},
{
name: 'Dundee'
}
],
E: [
{
name: 'Edinburgh'
},
{
name: 'Epsom'
}
],
G: [
{
name: 'Glasgow'
},
{
name: 'Gloucester'
}
],
H: [
{
name: 'Hull'
}
],
L: [
{
name: 'Leeds'
},
{
name: 'London'
}
]
}
try {
const sql = 'select name, organisation from organisation'
const result = await datasette.runQuery(sql)

const sortedResults = result.formattedData.sort((a, b) => {
return a.name.localeCompare(b.name)
})

res.render('organisations/find.html', { alphabetisedOrgs })
const alphabetisedOrgs = sortedResults.reduce((acc, current) => {
const firstLetter = current.name.charAt(0).toUpperCase()
acc[firstLetter] = acc[firstLetter] || []
acc[firstLetter].push(current)
return acc
}, {})

res.render('organisations/find.html', { alphabetisedOrgs })
} catch (err) {
logger.warn(err)
next(err)
}
},

async getDatasetTaskList (req, res, next) {
res.render('organisations/datasetTaskList.html')
}

}

export default organisationsController
1 change: 0 additions & 1 deletion src/views/organisations/find.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ <h1 class="govuk-heading-xl govuk-!-margin-bottom-5">{{ pageName }}</h1>

<div class="govuk-grid-row">
<div class="govuk-grid-column-full" id="search_results">

{% for letter, orgs in alphabetisedOrgs %}
<div class="govuk-grid-row js-filter-item" data-filter="block">
<div class="govuk-grid-column-one-third">
Expand Down
77 changes: 72 additions & 5 deletions test/unit/organisationsController.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { describe, it, vi, expect, beforeEach } from 'vitest'
import LpaOverviewController from '../../src/controllers/OrganisationsController.js'
import organisationsController from '../../src/controllers/OrganisationsController.js'
import performanceDbApi from '../../src/services/performanceDbApi.js'
import datasette from '../../src/services/datasette.js'

vi.mock('../../src/services/performanceDbApi.js')
vi.mock('../../src/utils/utils.js', () => {
return {
dataSubjects: {}
}
})
vi.mock('../../src/services/datasette.js')

describe('OrganisationsController.js', () => {
beforeEach(() => {
Expand All @@ -31,7 +33,7 @@ describe('OrganisationsController.js', () => {

performanceDbApi.getLpaOverview = vi.fn().mockResolvedValue(expectedResponse)

await LpaOverviewController.getOverview(req, res, next)
await organisationsController.getOverview(req, res, next)

expect(res.render).toHaveBeenCalledTimes(1)
expect(res.render).toHaveBeenCalledWith('organisations/overview.html', expect.objectContaining({
Expand All @@ -57,18 +59,83 @@ describe('OrganisationsController.js', () => {

vi.mocked(performanceDbApi.getLpaOverview).mockRejectedValue(error)

await LpaOverviewController.getOverview(req, res, next)
await organisationsController.getOverview(req, res, next)

expect(next).toHaveBeenCalledTimes(1)
expect(next).toHaveBeenCalledWith(error)
})
})

describe('find', () => {
it.todo('should render the find page', () => {
it('should call render with the find page', async () => {
const req = {}
const res = { render: vi.fn() }
const next = vi.fn()

vi.mocked(datasette.runQuery).mockResolvedValue({ formattedData: [] })

await organisationsController.getOrganisations(req, res, next)

expect(res.render).toHaveBeenCalledTimes(1)
expect(res.render).toHaveBeenCalledWith('organisations/find.html', expect.objectContaining({
alphabetisedOrgs: {}
}))
})

it('should correctly sort and restructure the data recieved from datasette, then pass it on to the template', async () => {
const req = {}
const res = { render: vi.fn() }
const next = vi.fn()

const datasetteResponse = [
{ name: 'Aardvark Healthcare', organisation: 'Aardvark Healthcare' },
{ name: 'Bath NHS Trust', organisation: 'Bath NHS Trust' },
{ name: 'Bristol Hospital', organisation: 'Bristol Hospital' },
{ name: 'Cardiff Health Board', organisation: 'Cardiff Health Board' },
{ name: 'Derbyshire Healthcare', organisation: 'Derbyshire Healthcare' },
{ name: 'East Sussex NHS Trust', organisation: 'East Sussex NHS Trust' }
]

vi.mocked(datasette.runQuery).mockResolvedValue({ formattedData: datasetteResponse })

await organisationsController.getOrganisations(req, res, next)

expect(res.render).toHaveBeenCalledTimes(1)
expect(res.render).toHaveBeenCalledWith('organisations/find.html', expect.objectContaining({
alphabetisedOrgs: {
A: [
{ name: 'Aardvark Healthcare', organisation: 'Aardvark Healthcare' }
],
B: [
{ name: 'Bath NHS Trust', organisation: 'Bath NHS Trust' },
{ name: 'Bristol Hospital', organisation: 'Bristol Hospital' }
],
C: [
{ name: 'Cardiff Health Board', organisation: 'Cardiff Health Board' }
],
D: [
{ name: 'Derbyshire Healthcare', organisation: 'Derbyshire Healthcare' }
],
E: [
{ name: 'East Sussex NHS Trust', organisation: 'East Sussex NHS Trust' }
]
}
}))
})

it.todo('should catch errors and pass them onto the next function')
it('should catch errors and pass them onto the next function', async () => {
const req = {}
const res = {}
const next = vi.fn()

const error = new Error('Test error')

vi.mocked(datasette.runQuery).mockRejectedValue(error)

await organisationsController.getOrganisations(req, res, next)

expect(next).toHaveBeenCalledTimes(1)
expect(next).toHaveBeenCalledWith(error)
})
})
})

0 comments on commit e254e64

Please sign in to comment.