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] setValue', () => {
    test('should set only insuranceCompany value', () => {
      const rootStore = new RootStore();
      const { insurance } = rootStore.$tables;

      const TEST_VALUE = 'TEST_VALUE';

      insurance.row('kasko').setValue('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).toEqual({
        key: 'kasko',
        policyType: 'КАСКО',
        insuranceCompany: TEST_VALUE,
        insured: DEFAULT_KASKO_VALUES.insured,
        insCost: DEFAULT_KASKO_VALUES.insCost,
        insTerm: DEFAULT_KASKO_VALUES.insTerm,
      });
    });
  });

  describe('[method] setOptions', () => {
    test('should replace only insuranceCompany options', () => {
      const rootStore = new RootStore();
      const { insurance } = rootStore.$tables;

      insurance.row('kasko').setOptions('insuranceCompany', INSURANCE_COMPANIES_OPTIONS);

      const { getOptions } = insurance.row('kasko');
      expect(getOptions('insuranceCompany')).toEqual(INSURANCE_COMPANIES_OPTIONS);
      expect(getOptions('insured')).toEqual(defaultOptions.kasko.insured);
      expect(getOptions('insTerm')).toEqual(defaultOptions.kasko.insTerm);
    });

    test('should replace insuranceCompany and insured options', () => {
      const rootStore = new RootStore();
      const { insurance } = rootStore.$tables;

      insurance
        .row('kasko')
        .setOptions('insuranceCompany', INSURANCE_COMPANIES_OPTIONS)
        .setOptions('insured', INSURED_OPTIONS);

      const { getOptions } = insurance.row('kasko');
      expect(getOptions('insuranceCompany')).toEqual(INSURANCE_COMPANIES_OPTIONS);
      expect(getOptions('insured')).toEqual(INSURED_OPTIONS);
      expect(getOptions('insTerm')).toEqual(defaultOptions.kasko.insTerm);
    });
  });

  describe('[method] block', () => {
    test('should replace only insuranceCompany and insured statuses', () => {
      const rootStore = new RootStore();
      const { insurance } = rootStore.$tables;

      insurance.row('kasko').block('insuranceCompany').block('insured');

      const { getStatus } = insurance.row('kasko');

      expect(getStatus('insTerm')).toEqual(defaultStatuses.kasko.insTerm);
      expect(getStatus('insCost')).toEqual(defaultStatuses.kasko.insCost);
      expect(getStatus('insuranceCompany')).toEqual('Disabled');
      expect(getStatus('insured')).toEqual('Disabled');
      expect(getStatus('policyType')).toEqual(defaultStatuses.kasko.policyType);
    });
  });
});
