-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ui): support Apollo caching for settings / Policies (#9442)
- Loading branch information
1 parent
32d237b
commit 288e458
Showing
4 changed files
with
460 additions
and
169 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
110 changes: 110 additions & 0 deletions
110
datahub-web-react/src/app/permissions/policy/_tests_/policyUtils.test.tsx
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,110 @@ | ||
import { | ||
addOrUpdatePoliciesInList, | ||
updateListPoliciesCache, | ||
removeFromListPoliciesCache, | ||
} from '../policyUtils'; | ||
|
||
// Mock the Apollo Client readQuery and writeQuery methods | ||
const mockReadQuery = jest.fn(); | ||
const mockWriteQuery = jest.fn(); | ||
|
||
jest.mock('@apollo/client', () => ({ | ||
...jest.requireActual('@apollo/client'), | ||
useApolloClient: () => ({ | ||
readQuery: mockReadQuery, | ||
writeQuery: mockWriteQuery, | ||
}), | ||
})); | ||
|
||
describe('addOrUpdatePoliciesInList', () => { | ||
it('should add a new policy to the list', () => { | ||
const existingPolicies = [{ urn: 'existing-urn' }]; | ||
const newPolicies = { urn: 'new-urn' }; | ||
|
||
const result = addOrUpdatePoliciesInList(existingPolicies, newPolicies); | ||
|
||
expect(result.length).toBe(existingPolicies.length + 1); | ||
expect(result).toContain(newPolicies); | ||
}); | ||
|
||
it('should update an existing policy in the list', () => { | ||
const existingPolicies = [{ urn: 'existing-urn' }]; | ||
const newPolicies = { urn: 'existing-urn', updatedField: 'new-value' }; | ||
|
||
const result = addOrUpdatePoliciesInList(existingPolicies, newPolicies); | ||
|
||
expect(result.length).toBe(existingPolicies.length); | ||
expect(result).toContainEqual(newPolicies); | ||
}); | ||
}); | ||
|
||
describe('updateListPoliciesCache', () => { | ||
// Mock client.readQuery response | ||
const mockReadQueryResponse = { | ||
listPolicies: { | ||
start: 0, | ||
count: 1, | ||
total: 1, | ||
policies: [{ urn: 'existing-urn' }], | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
mockReadQuery.mockReturnValueOnce(mockReadQueryResponse); | ||
}); | ||
|
||
it('should update the list policies cache with a new policy', () => { | ||
const mockClient = { | ||
readQuery: mockReadQuery, | ||
writeQuery: mockWriteQuery, | ||
}; | ||
|
||
const policiesToAdd = [{ urn: 'new-urn' }]; | ||
const pageSize = 10; | ||
|
||
updateListPoliciesCache(mockClient, policiesToAdd, pageSize); | ||
|
||
// Ensure writeQuery is called with the expected data | ||
expect(mockWriteQuery).toHaveBeenCalledWith({ | ||
query: expect.any(Object), | ||
variables: { input: { start: 0, count: pageSize, query: undefined } }, | ||
data: expect.any(Object), | ||
}); | ||
}); | ||
}); | ||
|
||
describe('removeFromListPoliciesCache', () => { | ||
// Mock client.readQuery response | ||
const mockReadQueryResponse = { | ||
listPolicies: { | ||
start: 0, | ||
count: 1, | ||
total: 1, | ||
policies: [{ urn: 'existing-urn' }], | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
mockReadQuery.mockReturnValueOnce(mockReadQueryResponse); | ||
}); | ||
|
||
it('should remove a policy from the list policies cache', () => { | ||
const mockClient = { | ||
readQuery: mockReadQuery, | ||
writeQuery: mockWriteQuery, | ||
}; | ||
|
||
const urnToRemove = 'existing-urn'; | ||
const pageSize = 10; | ||
|
||
removeFromListPoliciesCache(mockClient, urnToRemove, pageSize); | ||
|
||
// Ensure writeQuery is called with the expected data | ||
expect(mockWriteQuery).toHaveBeenCalledWith({ | ||
query: expect.any(Object), | ||
variables: { input: { start: 0, count: pageSize } }, | ||
data: expect.any(Object), | ||
}); | ||
}); | ||
}); | ||
|
Oops, something went wrong.