Skip to content

Commit

Permalink
Merge pull request #41050 from tienifr/fix/40835
Browse files Browse the repository at this point in the history
  • Loading branch information
luacmartins authored May 15, 2024
2 parents f5fb0f8 + de74afd commit 8c7ec08
Show file tree
Hide file tree
Showing 4 changed files with 582 additions and 0 deletions.
255 changes: 255 additions & 0 deletions tests/actions/PolicyCategoryTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
import Onyx from 'react-native-onyx';
import CONST from '@src/CONST';
import OnyxUpdateManager from '@src/libs/actions/OnyxUpdateManager';
import * as Policy from '@src/libs/actions/Policy';
import ONYXKEYS from '@src/ONYXKEYS';
import createRandomPolicy from '../utils/collections/policies';
import createRandomPolicyCategories from '../utils/collections/policyCategory';
import * as TestHelper from '../utils/TestHelper';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';

OnyxUpdateManager();
describe('actions/PolicyCategory', () => {
beforeAll(() => {
Onyx.init({
keys: ONYXKEYS,
});
});

beforeEach(() => {
// @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript.
global.fetch = TestHelper.getGlobalFetchMock();
return Onyx.clear().then(waitForBatchedUpdates);
});

describe('setWorkspaceRequiresCategory', () => {
it('Enable require category', async () => {
const fakePolicy = createRandomPolicy(0);
fakePolicy.requiresCategory = false;

// @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript.
fetch.pause();
Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy);
Policy.setWorkspaceRequiresCategory(fakePolicy.id, true);
await waitForBatchedUpdates();
await new Promise<void>((resolve) => {
const connectionID = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`,
waitForCollectionCallback: false,
callback: (policy) => {
Onyx.disconnect(connectionID);
// Check if policy requiresCategory was updated with correct values
expect(policy?.requiresCategory).toBeTruthy();
expect(policy?.pendingFields?.requiresCategory).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE);
expect(policy?.errors?.requiresCategory).toBeFalsy();
resolve();
},
});
});
// @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript.
await fetch.resume();
await waitForBatchedUpdates();
await new Promise<void>((resolve) => {
const connectionID = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`,
waitForCollectionCallback: false,
callback: (policy) => {
Onyx.disconnect(connectionID);
// Check if the policy pendingFields was cleared
expect(policy?.pendingFields?.requiresCategory).toBeFalsy();
resolve();
},
});
});
});
});
describe('createWorkspaceCategories', () => {
it('Create a new policy category', async () => {
const fakePolicy = createRandomPolicy(0);
const fakeCategories = createRandomPolicyCategories(3);
const newCategoryName = 'New category';
// @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript.
fetch.pause();
Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy);
Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories);
Policy.createPolicyCategory(fakePolicy.id, newCategoryName);
await waitForBatchedUpdates();
await new Promise<void>((resolve) => {
const connectionID = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`,
waitForCollectionCallback: false,
callback: (policyCategories) => {
Onyx.disconnect(connectionID);
const newCategory = policyCategories?.[newCategoryName];

expect(newCategory?.name).toBe(newCategoryName);
expect(newCategory?.errors).toBeFalsy();

resolve();
},
});
});
// @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript.
await fetch.resume();
await waitForBatchedUpdates();
await new Promise<void>((resolve) => {
const connectionID = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`,
waitForCollectionCallback: false,
callback: (policyCategories) => {
Onyx.disconnect(connectionID);

const newCategory = policyCategories?.[newCategoryName];
expect(newCategory?.errors).toBeFalsy();
expect(newCategory?.pendingAction).toBeFalsy();

resolve();
},
});
});
});
});
describe('renameWorkspaceCategory', () => {
it('Rename category', async () => {
const fakePolicy = createRandomPolicy(0);
const fakeCategories = createRandomPolicyCategories(3);
const oldCategoryName = Object.keys(fakeCategories)[0];
const newCategoryName = 'Updated category';
// @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript.
fetch.pause();
Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy);
Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories);
Policy.renamePolicyCategory(fakePolicy.id, {
oldName: oldCategoryName,
newName: newCategoryName,
});
await waitForBatchedUpdates();
await new Promise<void>((resolve) => {
const connectionID = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`,
waitForCollectionCallback: false,
callback: (policyCategories) => {
Onyx.disconnect(connectionID);

expect(policyCategories?.[oldCategoryName]).toBeFalsy();
expect(policyCategories?.[newCategoryName]?.name).toBe(newCategoryName);
expect(policyCategories?.[newCategoryName]?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE);
expect(policyCategories?.[newCategoryName]?.pendingFields?.name).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE);

resolve();
},
});
});
// @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript.
await fetch.resume();
await waitForBatchedUpdates();
await new Promise<void>((resolve) => {
const connectionID = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`,
waitForCollectionCallback: false,
callback: (policyCategories) => {
Onyx.disconnect(connectionID);

expect(policyCategories?.[newCategoryName]?.pendingAction).toBeFalsy();
expect(policyCategories?.[newCategoryName]?.pendingFields?.name).toBeFalsy();

resolve();
},
});
});
});
});
describe('setWorkspaceCategoriesEnabled', () => {
it('Enable category', async () => {
const fakePolicy = createRandomPolicy(0);
const fakeCategories = createRandomPolicyCategories(3);
const categoryNameToUpdate = Object.keys(fakeCategories)[0];
const categoriesToUpdate = {
[categoryNameToUpdate]: {
name: categoryNameToUpdate,
enabled: true,
},
};
// @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript.
fetch.pause();
Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy);
Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories);
Policy.setWorkspaceCategoryEnabled(fakePolicy.id, categoriesToUpdate);
await waitForBatchedUpdates();
await new Promise<void>((resolve) => {
const connectionID = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`,
waitForCollectionCallback: false,
callback: (policyCategories) => {
Onyx.disconnect(connectionID);

expect(policyCategories?.[categoryNameToUpdate]?.enabled).toBeTruthy();
expect(policyCategories?.[categoryNameToUpdate]?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE);
expect(policyCategories?.[categoryNameToUpdate]?.pendingFields?.enabled).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE);
expect(policyCategories?.[categoryNameToUpdate]?.errors).toBeFalsy();
resolve();
},
});
});
// @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript.
await fetch.resume();
await waitForBatchedUpdates();
await new Promise<void>((resolve) => {
const connectionID = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`,
waitForCollectionCallback: false,
callback: (policyCategories) => {
Onyx.disconnect(connectionID);

expect(policyCategories?.[categoryNameToUpdate]?.pendingAction).toBeFalsy();
expect(policyCategories?.[categoryNameToUpdate]?.pendingFields?.enabled).toBeFalsy();

resolve();
},
});
});
});
});

describe('deleteWorkspaceCategories', () => {
it('Delete category', async () => {
const fakePolicy = createRandomPolicy(0);
const fakeCategories = createRandomPolicyCategories(3);
const categoryNameToDelete = Object.keys(fakeCategories)[0];
const categoriesToDelete = [categoryNameToDelete];
// @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript.
fetch.pause();
Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy);
Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories);
Policy.deleteWorkspaceCategories(fakePolicy.id, categoriesToDelete);
await waitForBatchedUpdates();
await new Promise<void>((resolve) => {
const connectionID = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`,
waitForCollectionCallback: false,
callback: (policyCategories) => {
Onyx.disconnect(connectionID);

expect(policyCategories?.[categoryNameToDelete]?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE);
resolve();
},
});
});
// @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript.
await fetch.resume();
await waitForBatchedUpdates();
await new Promise<void>((resolve) => {
const connectionID = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`,
waitForCollectionCallback: false,
callback: (policyCategories) => {
Onyx.disconnect(connectionID);
expect(policyCategories?.[categoryNameToDelete]).toBeFalsy();

resolve();
},
});
});
});
});
});
Loading

0 comments on commit 8c7ec08

Please sign in to comment.