import { mount } from '@vue/test-utils';
import ConfigPage from '../../src/views/FacilityConfigPage';
import makeStore from '../makeStore';

function makeWrapper(propsData = {}) {
  const store = makeStore();
  store.commit('facilityConfig/SET_STATE', {
    settings: {
      learnerCanEditUsername: false,
    },
  });
  return mount(ConfigPage, { propsData, store });
}

function getElements(wrapper) {
  return {
    cancelResetButton: () => wrapper.find('button[name="cancel"]'),
    checkbox: () => wrapper.find('input[class="k-checkbox-input"]'),
    confirmResetButton: () => wrapper.find('button[name="submit"]'),
    resetButton: () => wrapper.find('button[name="reset-settings"]'),
    saveButton: () => wrapper.find('button[name="save-settings"]'),
    confirmResetModal: () => wrapper.find({ name: 'KModal' }),
    form: () => wrapper.find('form'),
  };
}

describe('facility config page view', () => {
  function assertModalIsUp(wrapper) {
    const { confirmResetModal } = getElements(wrapper);
    expect(confirmResetModal().exists()).toEqual(true);
  }

  function assertModalIsDown(wrapper) {
    const { confirmResetModal } = getElements(wrapper);
    expect(!confirmResetModal().exists()).toEqual(true);
  }

  it('clicking checkboxes dispatches a modify action', () => {
    const wrapper = makeWrapper();
    const { checkbox } = getElements(wrapper);
    checkbox().trigger('click');
    expect(wrapper.vm.$store.state.facilityConfig.settings.learnerCanEditUsername).toEqual(true);
  });

  it('clicking save button dispatches a save action', async () => {
    const wrapper = makeWrapper();
    const { mock } = (wrapper.vm.saveFacilityConfig = jest.fn().mockResolvedValue());
    const { saveButton } = getElements(wrapper);
    saveButton().trigger('click');
    expect(mock.calls).toHaveLength(1);
  });

  it('clicking reset button brings up the confirmation modal', () => {
    const wrapper = makeWrapper();
    const { resetButton } = getElements(wrapper);
    assertModalIsDown(wrapper);
    resetButton().trigger('click');
    assertModalIsUp(wrapper);
  });

  it('canceling reset tears down the modal', () => {
    const wrapper = makeWrapper();
    const { resetButton, cancelResetButton } = getElements(wrapper);
    assertModalIsDown(wrapper);
    resetButton().trigger('click');
    assertModalIsUp(wrapper);
    cancelResetButton().trigger('click');
    assertModalIsDown(wrapper);
  });

  it('confirming reset calls the reset action and closes modal', () => {
    const wrapper = makeWrapper();
    const { resetButton, confirmResetModal } = getElements(wrapper);
    const { mock } = (wrapper.vm.resetFacilityConfig = jest.fn().mockResolvedValue());
    resetButton().trigger('click');
    assertModalIsUp(wrapper);
    confirmResetModal().vm.$emit('submit');
    expect(mock.calls).toHaveLength(1);
    assertModalIsDown(wrapper);
  });
  // not tested: notifications
});

@@ -1221,16 +1221,732 @@
 );%0A  %7D%0A%0A
+  it('has all of the settings', () =%3E %7B%0A    const wrapper = makeWrapper();%0A    const checkboxes = wrapper.findAll(%7B name: 'KCheckbox' %7D);%0A    expect(checkboxes.length).toEqual(7);%0A    const labels = %5B%0A      'Allow learners and coaches to edit their username',%0A      'Allow learners and coaches to change their password when signed in',%0A      'Allow learners and coaches to edit their full name',%0A      'Allow learners to create accounts',%0A      'Allow learners to sign in with no password',%0A      %22Show 'download' button with content%22,%0A      'Allow users to access content without signing in',%0A    %5D;%0A    labels.forEach((label, idx) =%3E %7B%0A      expect(checkboxes.at(idx).props().label).toEqual(label);%0A    %7D);%0A  %7D);%0A%0A
   it('cl

Add test for facilityconfigpage