-
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.
fix: Site build polling and latest build branch
Signed-off-by: Andrew Burnes <[email protected]>
- Loading branch information
Showing
21 changed files
with
2,390 additions
and
881 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,60 @@ | ||
import { useQuery, useQueryClient, useMutation } from '@tanstack/react-query'; | ||
import api from '@util/federalistApi'; | ||
|
||
// 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 { isPending, error, data } = useQuery({ | ||
queryKey: ['builds', parseInt(siteId, 10)], | ||
queryFn: () => | ||
api.fetchBuilds({ id: siteId }).then((builds) => getLatestBuildByBranch(builds)), | ||
refetchInterval: 10000, | ||
refetchIntervalInBackground: false, | ||
}); | ||
|
||
return { isPending, error, data }; | ||
} | ||
|
||
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([ | ||
ref.current.scrollIntoView({ | ||
behavior: 'smooth', | ||
block: 'start', | ||
}), | ||
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,85 @@ | ||
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).toBe(false)); | ||
|
||
expect(Array.isArray(result.current.data)).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).toBe(false)); | ||
expect(result.current.error instanceof Error).toBe(true); | ||
}); | ||
|
||
it('should mutate the builds on rebuild', async () => { | ||
const siteId = 1; | ||
const buildId = 1; | ||
const ref = { | ||
current: { | ||
scrollIntoView: jest.fn(), | ||
}, | ||
}; | ||
|
||
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, | ||
); | ||
}); | ||
}); |
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.