-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4663 from cloud-gov/fix-build-polling-and-latest-…
…build-branch fix: Site build polling and latest build branch
- Loading branch information
Showing
22 changed files
with
2,457 additions
and
879 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,8 @@ | ||
const buildsFetchStartedType = 'BUILDS_FETCH_STARTED'; | ||
const buildsReceivedType = 'BUILDS_RECEIVED'; | ||
const buildRestartedType = 'BUILD_RESTARTED'; | ||
|
||
const buildsFetchStarted = () => ({ | ||
type: buildsFetchStartedType, | ||
}); | ||
|
||
const buildsReceived = (builds) => ({ | ||
type: buildsReceivedType, | ||
builds, | ||
}); | ||
|
||
const buildRestarted = (build) => ({ | ||
type: buildRestartedType, | ||
build, | ||
}); | ||
|
||
export { | ||
buildsFetchStarted, | ||
buildsFetchStartedType, | ||
buildsReceived, | ||
buildsReceivedType, | ||
buildRestarted, | ||
buildRestartedType, | ||
}; | ||
export { buildRestarted, buildRestartedType }; |
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,61 @@ | ||
import { useQuery, useQueryClient, useMutation } from '@tanstack/react-query'; | ||
import api from '@util/federalistApi'; | ||
import { REFETCH_INTERVAL, shouldScrollIntoView } from './utils'; | ||
|
||
// Assumes the order of builds is based on createdAt desc | ||
export function getLatestBuildByBranch(builds) { | ||
const latestBranches = []; | ||
|
||
return builds.map((build) => { | ||
if (build.completedAt && !latestBranches.includes(build.branch)) { | ||
latestBranches.push(build.branch); | ||
return { ...build, latestForBranch: true }; | ||
} | ||
|
||
return { ...build, latestForBranch: false }; | ||
}); | ||
} | ||
|
||
export function useBuilds(siteId) { | ||
const { data, error, isPending, isPlaceholderData } = useQuery({ | ||
placeholderData: [], | ||
queryKey: ['builds', parseInt(siteId, 10)], | ||
queryFn: () => | ||
api.fetchBuilds({ id: siteId }).then((builds) => getLatestBuildByBranch(builds)), | ||
refetchInterval: REFETCH_INTERVAL, | ||
refetchIntervalInBackground: false, | ||
}); | ||
|
||
return { data, error, isPending, isPlaceholderData }; | ||
} | ||
|
||
export function useRebuild(siteId, buildId, ref) { | ||
const queryClient = useQueryClient(); | ||
|
||
const mutation = useMutation({ | ||
mutationFn: ({ buildId, siteId }) => api.restartBuild(buildId, siteId), | ||
onSuccess: () => { | ||
// Scroll to build history container ref | ||
// Invalidate and refetch | ||
return Promise.all([ | ||
shouldScrollIntoView(ref), | ||
queryClient.invalidateQueries({ | ||
queryKey: ['builds', parseInt(siteId, 10)], | ||
}), | ||
]); | ||
}, | ||
}); | ||
|
||
async function rebuildBranch() { | ||
return mutation.mutate({ | ||
buildId, | ||
siteId, | ||
}); | ||
} | ||
|
||
return { | ||
...mutation, | ||
queryClient, | ||
rebuildBranch, | ||
}; | ||
} |
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,128 @@ | ||
import { waitFor, renderHook } from '@testing-library/react'; | ||
import nock from 'nock'; | ||
import { spy } from 'sinon'; | ||
import { createTestQueryClient } from '@support/queryClient'; | ||
import { getSiteBuilds, getSiteBuildsError, postSiteBuild } from '@support/nocks'; | ||
import { useBuilds, useRebuild } from './useBuilds'; | ||
|
||
const createWrapper = createTestQueryClient(); | ||
|
||
function checkForLatest(builds) { | ||
const hasChecked = []; | ||
|
||
return builds.map((build) => { | ||
const { latestForBranch, branch, completedAt } = build; | ||
|
||
if (latestForBranch && completedAt) { | ||
expect(!hasChecked.includes(branch)).toBe(true); | ||
hasChecked.push(branch); | ||
} | ||
|
||
if (!latestForBranch && completedAt) { | ||
expect(hasChecked.includes(branch)).toBe(true); | ||
} | ||
}); | ||
} | ||
|
||
describe('useBuilds', () => { | ||
beforeEach(() => nock.cleanAll()); | ||
afterAll(() => nock.restore()); | ||
|
||
it('should fetch builds based on site id', async () => { | ||
const siteId = 1; | ||
await getSiteBuilds(siteId); | ||
|
||
const { result } = renderHook(() => useBuilds(siteId), { | ||
wrapper: createWrapper(), | ||
}); | ||
|
||
await waitFor(() => | ||
expect(!result.current.isPending && !result.current.isPlaceholderData).toBe(true), | ||
); | ||
|
||
expect(Array.isArray(result.current.data)).toBe(true); | ||
expect(result.current.data.length > 0).toBe(true); | ||
checkForLatest(result.current.data); | ||
}); | ||
|
||
it('should return error', async () => { | ||
const siteId = 1; | ||
await getSiteBuildsError(siteId, 'Something happened'); | ||
|
||
const { result } = renderHook(() => useBuilds(siteId), { | ||
wrapper: createWrapper(), | ||
}); | ||
|
||
await waitFor(() => | ||
expect(!result.current.isPending && !result.current.isPlaceholderData).toBe(true), | ||
); | ||
|
||
expect(result.current.error instanceof Error).toBe(true); | ||
}); | ||
|
||
it('should mutate the builds on rebuild and call scrollIntoView', async () => { | ||
const siteId = 1; | ||
const buildId = 1; | ||
Object.defineProperty(window, 'scrollY', { | ||
value: 500, | ||
}); | ||
const ref = { | ||
current: { | ||
scrollIntoView: jest.fn(), | ||
offsetTop: 100, | ||
}, | ||
}; | ||
|
||
postSiteBuild(siteId, buildId); | ||
|
||
const { result } = renderHook(() => useRebuild(siteId, buildId, ref), { | ||
wrapper: createWrapper(), | ||
}); | ||
|
||
const qcSpy = spy(result.current.queryClient, 'invalidateQueries'); | ||
|
||
const isFunction = result.current.rebuildBranch instanceof Function; | ||
expect(isFunction).toBe(true); | ||
|
||
await result.current.rebuildBranch(); | ||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
||
expect(ref.current.scrollIntoView).toHaveBeenCalledTimes(1); | ||
expect(qcSpy.calledOnceWith({ queryKey: ['builds', parseInt(siteId, 10)] })).toBe( | ||
true, | ||
); | ||
}); | ||
|
||
it('should mutate the builds on rebuild and not scrollIntoView', async () => { | ||
const siteId = 1; | ||
const buildId = 1; | ||
Object.defineProperty(window, 'scrollY', { | ||
value: 200, | ||
}); | ||
const ref = { | ||
current: { | ||
scrollIntoView: jest.fn(), | ||
offsetTop: 100, | ||
}, | ||
}; | ||
|
||
postSiteBuild(siteId, buildId); | ||
|
||
const { result } = renderHook(() => useRebuild(siteId, buildId, ref), { | ||
wrapper: createWrapper(), | ||
}); | ||
|
||
const qcSpy = spy(result.current.queryClient, 'invalidateQueries'); | ||
|
||
const isFunction = result.current.rebuildBranch instanceof Function; | ||
expect(isFunction).toBe(true); | ||
|
||
await result.current.rebuildBranch(); | ||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
||
expect(ref.current.scrollIntoView).toHaveBeenCalledTimes(0); | ||
expect(qcSpy.calledOnceWith({ queryKey: ['builds', parseInt(siteId, 10)] })).toBe( | ||
true, | ||
); | ||
}); | ||
}); |
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,14 @@ | ||
export const REFETCH_INTERVAL = 10000; | ||
|
||
export const shouldScrollIntoView = (ref) => { | ||
const offset = Math.abs(ref.current.offsetTop - window.scrollY); | ||
|
||
if (offset > 200) { | ||
return ref.current.scrollIntoView({ | ||
behavior: 'smooth', | ||
block: 'start', | ||
}); | ||
} | ||
|
||
return Promise.resolve(); | ||
}; |
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
Oops, something went wrong.