Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/libs/actions/Policy/Tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,15 @@ function importPolicyTags(policyID: string, tags: PolicyTag[]) {
API.write(WRITE_COMMANDS.IMPORT_TAGS_SPREADSHEET, parameters, onyxData);
}

function setWorkspaceTagEnabled(policyID: string, tagsToUpdate: Record<string, {name: string; enabled: boolean}>, tagListIndex: number) {
const policyTag = PolicyUtils.getTagLists(allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] ?? {})?.at(tagListIndex);
type SetWorkspaceTagEnabledProps = {
policyID: string;
tagsToUpdate: Record<string, {name: string; enabled: boolean}>;
tagListIndex: number;
policyTags: OnyxEntry<PolicyTagLists>;
};

function setWorkspaceTagEnabled({policyID, tagsToUpdate, tagListIndex, policyTags}: SetWorkspaceTagEnabledProps) {
const policyTag = PolicyUtils.getTagLists(policyTags ?? {})?.at(tagListIndex);

if (!policyTag || tagListIndex === -1) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/pages/workspace/tags/TagSettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function TagSettingsPage({route, navigation}: TagSettingsPageProps) {
setIsCannotDeleteOrDisableLastTagModalVisible(true);
return;
}
setWorkspaceTagEnabled(policyID, {[currentPolicyTag.name]: {name: currentPolicyTag.name, enabled: value}}, policyTag.orderWeight);
setWorkspaceTagEnabled({policyID, tagsToUpdate: {[currentPolicyTag.name]: {name: currentPolicyTag.name, enabled: value}}, tagListIndex: policyTag.orderWeight, policyTags});
};

const navigateToEditTag = () => {
Expand Down
8 changes: 4 additions & 4 deletions src/pages/workspace/tags/WorkspaceTagsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ function WorkspaceTagsPage({route}: WorkspaceTagsPageProps) {

const updateWorkspaceTagEnabled = useCallback(
(value: boolean, tagName: string) => {
setWorkspaceTagEnabled(policyID, {[tagName]: {name: tagName, enabled: value}}, 0);
setWorkspaceTagEnabled({policyID, tagsToUpdate: {[tagName]: {name: tagName, enabled: value}}, tagListIndex: 0, policyTags});
},
[policyID],
[policyID, policyTags],
);

const updateWorkspaceRequiresTag = useCallback(
Expand Down Expand Up @@ -523,7 +523,7 @@ function WorkspaceTagsPage({route}: WorkspaceTagsPageProps) {
return;
}
setSelectedTags([]);
setWorkspaceTagEnabled(policyID, tagsToDisable, 0);
setWorkspaceTagEnabled({policyID, tagsToUpdate: tagsToDisable, tagListIndex: 0, policyTags});
},
});
}
Expand All @@ -535,7 +535,7 @@ function WorkspaceTagsPage({route}: WorkspaceTagsPageProps) {
value: CONST.POLICY.BULK_ACTION_TYPES.ENABLE,
onSelected: () => {
setSelectedTags([]);
setWorkspaceTagEnabled(policyID, tagsToEnable, 0);
setWorkspaceTagEnabled({policyID, tagsToUpdate: tagsToEnable, tagListIndex: 0, policyTags});
},
});
}
Expand Down
8 changes: 4 additions & 4 deletions src/pages/workspace/tags/WorkspaceViewTagsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ function WorkspaceViewTagsPage({route}: WorkspaceViewTagsProps) {

const updateWorkspaceTagEnabled = useCallback(
(value: boolean, tagName: string) => {
setWorkspaceTagEnabled(policyID, {[tagName]: {name: tagName, enabled: value}}, route.params.orderWeight);
setWorkspaceTagEnabled({policyID, tagsToUpdate: {[tagName]: {name: tagName, enabled: value}}, tagListIndex: route.params.orderWeight, policyTags});
},
[policyID, route.params.orderWeight],
[policyID, route.params.orderWeight, policyTags],
);

const tagList = useMemo<TagListItem[]>(
Expand Down Expand Up @@ -282,7 +282,7 @@ function WorkspaceViewTagsPage({route}: WorkspaceViewTagsProps) {
return;
}
setSelectedTags([]);
setWorkspaceTagEnabled(policyID, tagsToDisable, route.params.orderWeight);
setWorkspaceTagEnabled({policyID, tagsToUpdate: tagsToDisable, tagListIndex: route.params.orderWeight, policyTags});
},
});
}
Expand All @@ -294,7 +294,7 @@ function WorkspaceViewTagsPage({route}: WorkspaceViewTagsProps) {
value: CONST.POLICY.BULK_ACTION_TYPES.ENABLE,
onSelected: () => {
setSelectedTags([]);
setWorkspaceTagEnabled(policyID, tagsToEnable, route.params.orderWeight);
setWorkspaceTagEnabled({policyID, tagsToUpdate: tagsToEnable, tagListIndex: route.params.orderWeight, policyTags});
},
});
}
Expand Down
189 changes: 107 additions & 82 deletions tests/actions/PolicyTagTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,8 @@ describe('actions/Policy', () => {
});
});

describe('SetPolicyTagsEnabled', () => {
it('set policy tag enable', () => {
describe('SetWorkspaceTagEnabled', () => {
it('set policy tag enable', async () => {
const fakePolicy = createRandomPolicy(0);
fakePolicy.areTagsEnabled = true;

Expand All @@ -416,62 +416,46 @@ describe('actions/Policy', () => {

mockFetch?.pause?.();

return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy)
.then(() => {
Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags);
})
.then(() => {
setWorkspaceTagEnabled(fakePolicy.id, tagsToUpdate, 0);
return waitForBatchedUpdates();
})
.then(
() =>
new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`,
waitForCollectionCallback: false,
callback: (policyTags) => {
Onyx.disconnect(connection);
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy);
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags);

Object.keys(tagsToUpdate).forEach((key) => {
const updatedTag = policyTags?.[tagListName]?.tags[key];
expect(updatedTag?.enabled).toBeFalsy();
expect(updatedTag?.errors).toBeFalsy();
expect(updatedTag?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE);
expect(updatedTag?.pendingFields?.enabled).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE);
});
setWorkspaceTagEnabled({policyID: fakePolicy.id, tagsToUpdate, tagListIndex: 0, policyTags: fakePolicyTags});
await waitForBatchedUpdates();

resolve();
},
});
}),
)
.then(mockFetch?.resume)
.then(waitForBatchedUpdates)
.then(
() =>
new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`,
waitForCollectionCallback: false,
callback: (policyTags) => {
Onyx.disconnect(connection);
// Check optimistic updates
let optimisticPolicyTags: PolicyTagLists | undefined;
await TestHelper.getOnyxData({
key: `${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`,
callback: (val) => (optimisticPolicyTags = val),
});

Object.keys(tagsToUpdate).forEach((key) => {
const updatedTag = policyTags?.[tagListName]?.tags[key];
expect(updatedTag?.errors).toBeFalsy();
expect(updatedTag?.pendingAction).toBeFalsy();
expect(updatedTag?.pendingFields?.enabled).toBeFalsy();
});
Object.keys(tagsToUpdate).forEach((key) => {
const updatedTag = optimisticPolicyTags?.[tagListName]?.tags[key];
expect(updatedTag?.enabled).toBeFalsy();
expect(updatedTag?.errors).toBeFalsy();
expect(updatedTag?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE);
expect(updatedTag?.pendingFields?.enabled).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE);
});

resolve();
},
});
}),
);
mockFetch?.resume?.();
await waitForBatchedUpdates();

// Check success updates
let successPolicyTags: PolicyTagLists | undefined;
await TestHelper.getOnyxData({
key: `${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`,
callback: (val) => (successPolicyTags = val),
});

Object.keys(tagsToUpdate).forEach((key) => {
const updatedTag = successPolicyTags?.[tagListName]?.tags[key];
expect(updatedTag?.errors).toBeFalsy();
expect(updatedTag?.pendingAction).toBeFalsy();
expect(updatedTag?.pendingFields?.enabled).toBeFalsy();
});
});

it('reset policy tag enable when api returns error', () => {
it('reset policy tag enable when api returns error', async () => {
const fakePolicy = createRandomPolicy(0);
fakePolicy.areTagsEnabled = true;

Expand All @@ -487,39 +471,80 @@ describe('actions/Policy', () => {

mockFetch?.pause?.();

return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy)
.then(() => {
Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags);
})
.then(() => {
mockFetch?.fail?.();
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy);
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags);

setWorkspaceTagEnabled(fakePolicy.id, tagsToUpdate, 0);
return waitForBatchedUpdates();
})
.then(mockFetch?.resume)
.then(waitForBatchedUpdates)
.then(
() =>
new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`,
waitForCollectionCallback: false,
callback: (policyTags) => {
Onyx.disconnect(connection);
mockFetch?.fail?.();
setWorkspaceTagEnabled({policyID: fakePolicy.id, tagsToUpdate, tagListIndex: 0, policyTags: fakePolicyTags});
await waitForBatchedUpdates();

Object.keys(tagsToUpdate).forEach((key) => {
const updatedTag = policyTags?.[tagListName]?.tags[key];
expect(updatedTag?.errors).toBeTruthy();
expect(updatedTag?.pendingAction).toBeFalsy();
expect(updatedTag?.pendingFields?.enabled).toBeFalsy();
});
mockFetch?.resume?.();
await waitForBatchedUpdates();

resolve();
},
});
}),
);
// Check failure updates
let failurePolicyTags: PolicyTagLists | undefined;
await TestHelper.getOnyxData({
key: `${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`,
callback: (val) => (failurePolicyTags = val),
});

Object.keys(tagsToUpdate).forEach((key) => {
const updatedTag = failurePolicyTags?.[tagListName]?.tags[key];
expect(updatedTag?.errors).toBeTruthy();
expect(updatedTag?.pendingAction).toBeFalsy();
expect(updatedTag?.pendingFields?.enabled).toBeFalsy();
});
});

it('should work with data from useOnyx hook', async () => {
const fakePolicy = createRandomPolicy(0);
const tagListName = 'Test tag';
const fakePolicyTags = createRandomPolicyTags(tagListName, 1);
const tagName = Object.keys(fakePolicyTags?.[tagListName]?.tags ?? {}).at(0) ?? '';

mockFetch?.pause?.();

await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags);

const {result} = renderHook(() => useOnyx(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`));

await waitFor(() => {
expect(result.current[0]).toBeDefined();
});

setWorkspaceTagEnabled({
policyID: fakePolicy.id,
tagsToUpdate: {[tagName]: {name: tagName, enabled: false}},
tagListIndex: 0,
policyTags: result.current[0],
});

await waitForBatchedUpdates();

// Check optimistic updates
let optimisticPolicyTags: PolicyTagLists | undefined;
await TestHelper.getOnyxData({
key: `${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`,
callback: (val) => (optimisticPolicyTags = val),
});

const optimisticTag = optimisticPolicyTags?.[tagListName]?.tags[tagName];
expect(optimisticTag?.enabled).toBe(false);
expect(optimisticTag?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE);

// Check success updates
mockFetch?.resume?.();
await waitForBatchedUpdates();

let successPolicyTags: PolicyTagLists | undefined;
await TestHelper.getOnyxData({
key: `${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`,
callback: (val) => (successPolicyTags = val),
});

const successTag = successPolicyTags?.[tagListName]?.tags[tagName];
expect(successTag?.enabled).toBe(false);
expect(successTag?.pendingAction).toBeFalsy();
});
});

Expand Down