From f082b834fd329cc12c53d532351ee66ebb31923c Mon Sep 17 00:00:00 2001 From: Chika Date: Sat, 17 Sep 2022 19:59:12 +0300 Subject: [PATCH] tests: add stores/tables/insurance tests stores: fix stores/tables/insurance methods --- .../tables/insurance/__tests__/index.test.js | 145 ++++++++++++++++++ stores/tables/insurance/index.ts | 12 +- 2 files changed, 152 insertions(+), 5 deletions(-) create mode 100644 stores/tables/insurance/__tests__/index.test.js diff --git a/stores/tables/insurance/__tests__/index.test.js b/stores/tables/insurance/__tests__/index.test.js new file mode 100644 index 0000000..8ac840d --- /dev/null +++ b/stores/tables/insurance/__tests__/index.test.js @@ -0,0 +1,145 @@ +import { defaultOptions, defaultStatuses, defaultValues } from 'config/tables/insurance-table'; +import { toJS } from 'mobx'; +import RootStore from 'stores/root'; + +const INSURANCE_COMPANIES_OPTIONS = [ + { + label: 'TEST', + value: 'TEST', + }, + { + label: 'TEST2', + value: 'TEST2', + }, +]; + +const INSURED_OPTIONS = [ + { + label: 'TEST', + value: 'TEST', + }, +]; + +describe('stores/tables/insurance', () => { + describe('[method] setRowValues', () => { + it('should set only insuranceCompany value', () => { + const rootStore = new RootStore(); + const { insurance } = rootStore.$tables; + + const TEST_VALUE = 'TEST_VALUE'; + + insurance.setRowValues('kasko', { + insuranceCompany: TEST_VALUE, + }); + + const KASKO_VALUES = toJS(insurance.values.find((x) => x.key === 'kasko')); + const DEFAULT_KASKO_VALUES = defaultValues.find((x) => x.key === 'kasko'); + expect(KASKO_VALUES.insuranceCompany).toEqual(TEST_VALUE); + expect(KASKO_VALUES.insured).toEqual(DEFAULT_KASKO_VALUES.insured); + expect(KASKO_VALUES.insTerm).toEqual(DEFAULT_KASKO_VALUES.insTerm); + }); + }); + + describe('[method] setRowOptions', () => { + it('should replace only insuranceCompany options', () => { + const rootStore = new RootStore(); + const { insurance } = rootStore.$tables; + + insurance.setRowOptions('kasko', { + insuranceCompany: INSURANCE_COMPANIES_OPTIONS, + }); + + const KASKO = toJS(insurance.getRowOptions('kasko')); + + expect(KASKO.insuranceCompany).toEqual(INSURANCE_COMPANIES_OPTIONS); + expect(KASKO.insured).toEqual(defaultOptions.kasko.insured); + expect(KASKO.insTerm).toEqual(defaultOptions.kasko.insTerm); + }); + + it('should replace insuranceCompany and insured options', () => { + const rootStore = new RootStore(); + const { insurance } = rootStore.$tables; + + insurance.setRowOptions('kasko', { + insuranceCompany: INSURANCE_COMPANIES_OPTIONS, + insured: INSURED_OPTIONS, + }); + + const KASKO = toJS(insurance.getRowOptions('kasko')); + + expect(KASKO.insuranceCompany).toEqual(INSURANCE_COMPANIES_OPTIONS); + expect(KASKO.insured).toEqual(INSURED_OPTIONS); + expect(KASKO.insTerm).toEqual(defaultOptions.kasko.insTerm); + }); + }); + + describe('[method] setManyRowOptions', () => { + it('should replace only kasko options', () => { + const rootStore = new RootStore(); + const { insurance } = rootStore.$tables; + + insurance.setManyRowOptions({ + kasko: { + insuranceCompany: INSURANCE_COMPANIES_OPTIONS, + insured: INSURED_OPTIONS, + }, + }); + + const KASKO = toJS(insurance.getRowOptions('kasko')); + const OSAGO = toJS(insurance.getRowOptions('osago')); + const FINGAP = toJS(insurance.getRowOptions('fingap')); + + expect(KASKO.insuranceCompany).toEqual(INSURANCE_COMPANIES_OPTIONS); + expect(KASKO.insured).toEqual(INSURED_OPTIONS); + expect(KASKO.insTerm).toEqual(defaultOptions.kasko.insTerm); + expect(OSAGO).toEqual(defaultOptions.osago); + expect(FINGAP).toEqual(defaultOptions.fingap); + }); + + it('should replace only kasko and osago options', () => { + const rootStore = new RootStore(); + const { insurance } = rootStore.$tables; + + insurance.setManyRowOptions({ + kasko: { + insuranceCompany: INSURANCE_COMPANIES_OPTIONS, + insured: INSURED_OPTIONS, + }, + osago: { + insured: INSURED_OPTIONS, + }, + }); + + const KASKO = toJS(insurance.getRowOptions('kasko')); + const OSAGO = toJS(insurance.getRowOptions('osago')); + const FINGAP = toJS(insurance.getRowOptions('fingap')); + + expect(KASKO.insuranceCompany).toEqual(INSURANCE_COMPANIES_OPTIONS); + expect(KASKO.insured).toEqual(INSURED_OPTIONS); + expect(KASKO.insTerm).toEqual(defaultOptions.kasko.insTerm); + expect(OSAGO.insured).toEqual(INSURED_OPTIONS); + expect(OSAGO.insTerm).toEqual(defaultOptions.osago.insTerm); + expect(FINGAP).toEqual(defaultOptions.fingap); + }); + }); + + describe('[method] setRowStatuses', () => { + it('should replace only insuranceCompany and insured statuses', () => { + const rootStore = new RootStore(); + const { insurance } = rootStore.$tables; + + insurance.setRowStatuses('kasko', { + insuranceCompany: 'Disabled', + insured: 'Disabled', + }); + + const KASKO = toJS(insurance.getRowStatuses('kasko')); + + expect(KASKO.insuranceCompany).toEqual('Disabled'); + expect(KASKO.insured).toEqual('Disabled'); + expect(KASKO.insCost).toEqual(defaultStatuses.kasko.insCost); + expect(KASKO.insTerm).toEqual(defaultStatuses.kasko.insTerm); + expect(KASKO.policyType).toEqual(defaultStatuses.kasko.policyType); + }); + }); +}); diff --git a/stores/tables/insurance/index.ts b/stores/tables/insurance/index.ts index 9808fae..246ce3e 100644 --- a/stores/tables/insurance/index.ts +++ b/stores/tables/insurance/index.ts @@ -1,7 +1,6 @@ /* eslint-disable object-curly-newline */ import type * as Insurance from 'Components/Calculation/Form/Insurance/InsuranceTable/types'; import * as insuranceTableConfig from 'config/tables/insurance-table'; -import { mergeWith } from 'lodash-es'; import { makeAutoObservable } from 'mobx'; import type RootStore from 'stores/root'; import Validation from '../validation'; @@ -48,7 +47,7 @@ export default class InsuranceTable { const rowIndex = this.values.findIndex((x) => x.key === key); if (rowIndex >= 0) { - mergeWith(this.values[rowIndex], rowValues); + this.values[rowIndex] = { ...this.values[rowIndex], ...rowValues }; } }; @@ -57,11 +56,14 @@ export default class InsuranceTable { } setRowOptions = (key: Insurance.Keys, rowOptions: Insurance.RowOptions) => { - mergeWith(this.options[key], rowOptions); + this.options[key] = { ...this.options[key], ...rowOptions }; }; setManyRowOptions = (options: Partial>) => { - mergeWith(this.options, options); + (Object.keys(options) as Insurance.Keys[]).forEach((key) => { + const rowOptions = options[key]; + if (rowOptions !== undefined) this.setRowOptions(key, rowOptions); + }); }; getRowStatuses(key: Insurance.Keys) { @@ -69,7 +71,7 @@ export default class InsuranceTable { } setRowStatuses = (key: Insurance.Keys, rowStatuses: Insurance.RowStatuses) => { - mergeWith(this.statuses[key], rowStatuses); + this.statuses[key] = { ...this.statuses[key], ...rowStatuses }; }; reset = () => {