Skip to content

Commit

Permalink
feat: prefer resolveTo and rejectWith instead of returnValue(Promise.…
Browse files Browse the repository at this point in the history
…resolve()) (#2912)

* feat: prefer resolveTo and rejectWith instead of returnValue(Promise.resolve())

* feat: added deepFreeze to not pollute global mock data (#2913)

* feat: added deepFreeze to not pollute global mock data

* feat: added deepFreeze to not pollute global mock data - Part 2 (#2914)

* feat: added deepFreeze to not pollute global mock data - Part 2

* feat: added deepFreeze to not pollute global mock data - Part 3 (#2915)

* feat: added deepFreeze to not pollute global mock data - Part 3

* feat: added deepFreeze to not pollute global mock data - Part 4 (#2916)

* feat: added deepFreeze to not pollute global mock data - Part 4

* feat: added deepFreeze to not pollute global mock data - Part 5 (#2917)

* feat: added deepFreeze to not pollute global mock data - Part 5

* feat: added deepFreeze to not pollute global mock data - Part 6 (#2918)

* feat: added deepFreeze to not pollute global mock data - Part 6

* feat: added deepFreeze to not pollute global mock data - Part 7 (#2919)

* feat: added deepFreeze to not pollute global mock data - Part 7

* minor

* feat: added deepFreeze to not pollute global mock data - Part 8 (#2922)

* feat: added deepFreeze to not pollute global mock data - Part 8

* feat: added deepFreeze to not pollute global mock data - Part 9 (#2924)

* feat: added deepFreeze to not pollute global mock data - Part 9

* feat: added deepFreeze to not pollute global mock data - Part 10 (#2925)

* feat: added deepFreeze to not pollute global mock data - Part 10

* feat: added deepFreeze to not pollute global mock data - Part 11 (#2927)

* feat: added deepFreeze to not pollute global mock data - Part 11

* feat: added deepFreeze to not pollute global mock data - Part 12 (#2929)

* feat: added deepFreeze to not pollute global mock data - Part 12

* feat: added deepFreeze to not pollute global mock data - Part 13 (#2930)

* feat: added deepFreeze to not pollute global mock data - Part 13

* minor

* feat: added deepFreeze to not pollute global mock data - Part 14 (#2933)

* feat: added deepFreeze to not pollute global mock data - Part 14

* feat: added deepFreeze to not pollute global mock data - Part 15 (#2934)

* feat: added deepFreeze to not pollute global mock data - Part 15

* test: fixing tests - part 1 (#2939)

* test: fixing tests - part 1

* test: fixing tests - part 2 (#2940)

* test: fixing tests - part 2

* test: fixing tests - part 3 (#2941)

* test: fixing tests - part 3

* test: fixing tests - part 4 (#2943)

* test: fixing tests - part 4

* test: fixing tests - part 5 (#2945)

* minor
  • Loading branch information
suyashpatil78 authored May 4, 2024
1 parent e306edb commit a111842
Show file tree
Hide file tree
Showing 262 changed files with 3,717 additions and 3,089 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@
"@typescript-eslint/no-inferrable-types": "off",
"no-unused-expressions": "off",
"custom-rules/space-before-it-blocks": "error",
"custom-rules/prefer-jasmine-matchers": "error"
"custom-rules/prefer-jasmine-matchers": "error",
"custom-rules/prefer-resolve-to-reject-with": "error"
}
}
]
Expand Down
1 change: 1 addition & 0 deletions eslint-custom-rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ module.exports = {
'prefer-deep-freeze': require('./rules/eslint-plugin-prefer-deep-freeze'),
'space-before-it-blocks': require('./rules/eslint-plugin-space-before-it-blocks'),
'prefer-jasmine-matchers': require('./rules/eslint-plugin-prefer-jasmine-matchers'),
'prefer-resolve-to-reject-with': require('./rules/eslint-plugin-prefer-resolve-to-reject-with')
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const SpyStrategyCall = `CallExpression:matches(
[callee.object.property.name=and],
[callee.object.callee.property.name=withArgs]
)`.replace(/\s+/g, ' ');

const ReturnStrategy = `${SpyStrategyCall}[callee.property.name=returnValue]`;

// Matches Promise.{resolve,reject}(X)
const PromiseCall = 'CallExpression[callee.object.name=Promise]';
const SettledPromise = `${PromiseCall}[callee.property.name=/resolve|reject/]`;

module.exports = {
meta: {
type: 'suggestion',
docs: {
description: "Enforce using resolveTo() and rejectWith() instead of Promise.resolve() and Promise.reject() in Jasmine tests",
category: "Best Practices",
recommended: true,
},
fixable: 'code',
schema: []
},

create: context => ({
[`${ReturnStrategy} > ${SettledPromise}.arguments:first-child`] (promiseCall) {
const returnStrategyCall = promiseCall.parent;
const returnValueMethod = returnStrategyCall.callee.property;
const preferredMethod = promiseCall.callee.property.name === 'resolve'
? 'resolveTo' : 'rejectWith';

context.report({
message: `Prefer ${preferredMethod}`,
loc: {
start: returnValueMethod.loc.start,
end: returnStrategyCall.loc.end
},
fix (fixer) {
const code = context.getSourceCode();
return [
// Replace Promise constructor call with its arguments
fixer.remove(promiseCall.callee),
fixer.remove(code.getTokenAfter(promiseCall.callee)),
fixer.remove(code.getLastToken(promiseCall)),

// Replace returnValue method with resolveTo or rejectWith
fixer.replaceText(returnValueMethod, preferredMethod)
];
}
})
}
})
};
6 changes: 4 additions & 2 deletions src/app/core/mock-data/account-option.data.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import deepFreeze from 'deep-freeze-strict';

import { AccountOption } from '../models/account-option.model';
import { multiplePaymentModesData } from '../test-data/accounts.service.spec.data';

export const accountOptionData1: AccountOption[] = [
export const accountOptionData1: AccountOption[] = deepFreeze([
{
label: 'account1',
value: multiplePaymentModesData[0],
Expand All @@ -10,4 +12,4 @@ export const accountOptionData1: AccountOption[] = [
label: 'account2',
value: multiplePaymentModesData[1],
},
];
]);
10 changes: 6 additions & 4 deletions src/app/core/mock-data/acess-token-data.data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import deepFreeze from 'deep-freeze-strict';

import { AccessTokenData } from '../models/access-token-data.model';

export const apiAccessTokenRes: AccessTokenData = {
export const apiAccessTokenRes: AccessTokenData = deepFreeze({
iat: 1678349549,
iss: 'FyleApp',
user_id: 'usvKA4X8Ugcr',
Expand All @@ -12,9 +14,9 @@ export const apiAccessTokenRes: AccessTokenData = {
version: '3',
cluster_domain: '"https://staging.fyle.tech"',
exp: 1678353149,
};
});

export const apiTokenWithoutRoles: AccessTokenData = {
export const apiTokenWithoutRoles: AccessTokenData = deepFreeze({
iat: 1678349549,
iss: 'FyleApp',
user_id: 'usvKA4X8Ugcr',
Expand All @@ -25,4 +27,4 @@ export const apiTokenWithoutRoles: AccessTokenData = {
version: '3',
cluster_domain: '"https://staging.fyle.tech"',
exp: 1678353149,
};
});
18 changes: 10 additions & 8 deletions src/app/core/mock-data/action-sheet-options.data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const actionSheetOptionsData = [
import deepFreeze from 'deep-freeze-strict';

export const actionSheetOptionsData = deepFreeze([
{
text: 'Split Expense By Category',
handler: () => {},
Expand All @@ -19,8 +21,8 @@ export const actionSheetOptionsData = [
text: 'Remove Card Expense',
handler: () => {},
},
];
export const expectedActionSheetButtonRes = [
]);
export const expectedActionSheetButtonRes = deepFreeze([
{
text: 'Capture Receipt',
icon: 'assets/svg/camera.svg',
Expand All @@ -45,9 +47,9 @@ export const expectedActionSheetButtonRes = [
cssClass: 'capture-receipt',
handler: undefined,
},
];
]);

export const expectedActionSheetButtonsWithMileage = [
export const expectedActionSheetButtonsWithMileage = deepFreeze([
{
text: 'Capture Receipt',
icon: 'assets/svg/camera.svg',
Expand All @@ -66,9 +68,9 @@ export const expectedActionSheetButtonsWithMileage = [
cssClass: 'capture-receipt',
handler: undefined,
},
];
]);

export const expectedActionSheetButtonsWithPerDiem = [
export const expectedActionSheetButtonsWithPerDiem = deepFreeze([
{
text: 'Capture Receipt',
icon: 'assets/svg/camera.svg',
Expand All @@ -87,4 +89,4 @@ export const expectedActionSheetButtonsWithPerDiem = [
cssClass: 'capture-receipt',
handler: undefined,
},
];
]);
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import deepFreeze from 'deep-freeze-strict';

import { AddEditAdvanceRequestFormValue } from '../models/add-edit-advance-request-form-value.model';
import { recentlyUsedProjectRes } from './recently-used.data';

export const addEditAdvanceRequestFormValueData: AddEditAdvanceRequestFormValue = {
export const addEditAdvanceRequestFormValueData: AddEditAdvanceRequestFormValue = deepFreeze({
currencyObj: {
amount: 130,
currency: 'USD',
Expand All @@ -12,17 +14,17 @@ export const addEditAdvanceRequestFormValueData: AddEditAdvanceRequestFormValue
notes: 'Test notes',
project: null,
customFieldValues: null,
};
});

export const addEditAdvanceRequestFormValueData2: AddEditAdvanceRequestFormValue = {
export const addEditAdvanceRequestFormValueData2: AddEditAdvanceRequestFormValue = deepFreeze({
currencyObj: null,
purpose: null,
notes: null,
project: null,
customFieldValues: [],
};
});

export const addEditAdvanceRequestFormValueData3: AddEditAdvanceRequestFormValue = {
export const addEditAdvanceRequestFormValueData3: AddEditAdvanceRequestFormValue = deepFreeze({
...addEditAdvanceRequestFormValueData,
project: recentlyUsedProjectRes[0],
};
});
6 changes: 4 additions & 2 deletions src/app/core/mock-data/advance-platform.data.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import deepFreeze from 'deep-freeze-strict';

import { CustomFieldTypes } from '../enums/platform/v1/custom-fields-type.enum';
import { AdvancesPlatform } from '../models/platform/advances-platform.model';
import { PlatformApiResponse } from '../models/platform/platform-api-response.model';

export const advancePlatform: PlatformApiResponse<AdvancesPlatform> = {
export const advancePlatform: PlatformApiResponse<AdvancesPlatform> = deepFreeze({
count: 1,
offset: 0,
data: [
Expand Down Expand Up @@ -85,4 +87,4 @@ export const advancePlatform: PlatformApiResponse<AdvancesPlatform> = {
},
},
],
};
});
6 changes: 4 additions & 2 deletions src/app/core/mock-data/advance-request-actions.data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import deepFreeze from 'deep-freeze-strict';

import { AdvanceRequestActions } from '../models/advance-request-actions.model';

export const apiAdvanceRequestAction: AdvanceRequestActions = {
export const apiAdvanceRequestAction: AdvanceRequestActions = deepFreeze({
id: 'areqoVuT5I8OOy',
can_save: true,
can_submit: true,
Expand All @@ -13,4 +15,4 @@ export const apiAdvanceRequestAction: AdvanceRequestActions = {
can_pull_back: false,
can_pay: false,
can_delete: true,
};
});
6 changes: 4 additions & 2 deletions src/app/core/mock-data/advance-request-approver.data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import deepFreeze from 'deep-freeze-strict';

import { AdvanceApprover } from '../models/approver.model';

export const AdvanceRequestApprover: AdvanceApprover[] = [
export const AdvanceRequestApprover: AdvanceApprover[] = deepFreeze([
{
id: 8311,
created_at: null,
Expand All @@ -15,4 +17,4 @@ export const AdvanceRequestApprover: AdvanceApprover[] = [
approver_org_id: 'orYtMVz2qisQ',
comment: null,
},
];
]);
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import deepFreeze from 'deep-freeze-strict';

import { AdvanceRequestCustomFieldValues } from '../models/advance-request-custom-field-values.model';

export const advanceRequestCustomFieldValuesData: AdvanceRequestCustomFieldValues[] = [
export const advanceRequestCustomFieldValuesData: AdvanceRequestCustomFieldValues[] = deepFreeze([
{
name: 'Phase',
value: 'Phase 1',
Expand All @@ -21,9 +23,9 @@ export const advanceRequestCustomFieldValuesData: AdvanceRequestCustomFieldValue
value: 'option1',
type: 'OPTION',
},
];
]);

export const advanceRequestCustomFieldValuesData2: AdvanceRequestCustomFieldValues[] = [
export const advanceRequestCustomFieldValuesData2: AdvanceRequestCustomFieldValues[] = deepFreeze([
{
name: 'Phase',
value: 'Phase 1',
Expand All @@ -40,4 +42,4 @@ export const advanceRequestCustomFieldValuesData2: AdvanceRequestCustomFieldValu
name: 'Checking',
value: 'option1',
},
];
]);
10 changes: 6 additions & 4 deletions src/app/core/mock-data/advance-request-file.data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import deepFreeze from 'deep-freeze-strict';

import { AdvanceRequestFile } from '../models/advance-request-file.model';

export const advRequestFile: AdvanceRequestFile = {
export const advRequestFile: AdvanceRequestFile = deepFreeze({
files: [
{
id: 'fi1w2IE6JeqS',
Expand Down Expand Up @@ -92,9 +94,9 @@ export const advRequestFile: AdvanceRequestFile = {
is_sent_back: false,
is_pulled_back: true,
},
};
});

export const advRequestFile2: AdvanceRequestFile = {
export const advRequestFile2: AdvanceRequestFile = deepFreeze({
files: [
{
id: 'fiK7c69UDJNb',
Expand Down Expand Up @@ -186,4 +188,4 @@ export const advRequestFile2: AdvanceRequestFile = {
is_sent_back: false,
is_pulled_back: true,
},
};
});
10 changes: 6 additions & 4 deletions src/app/core/mock-data/advance-requests-custom-fields.data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import deepFreeze from 'deep-freeze-strict';

import { AdvanceRequestsCustomFields } from '../models/advance-requests-custom-fields.model';
export const advanceRequestCustomFieldData: AdvanceRequestsCustomFields[] = [
export const advanceRequestCustomFieldData: AdvanceRequestsCustomFields[] = deepFreeze([
{
id: 150,
org_id: 'orNVthTo2Zyo',
Expand Down Expand Up @@ -42,9 +44,9 @@ export const advanceRequestCustomFieldData: AdvanceRequestsCustomFields[] = [
last_updated_by: 'ouX8dwsbLCLv',
placeholder: '123',
},
];
]);

export const advanceRequestCustomFieldData2: AdvanceRequestsCustomFields[] = [
export const advanceRequestCustomFieldData2: AdvanceRequestsCustomFields[] = deepFreeze([
{
id: 150,
org_id: 'orNVthTo2Zyo',
Expand Down Expand Up @@ -87,4 +89,4 @@ export const advanceRequestCustomFieldData2: AdvanceRequestsCustomFields[] = [
last_updated_by: 'ouX8dwsbLCLv',
placeholder: null,
},
];
]);
Loading

0 comments on commit a111842

Please sign in to comment.