import { createAppLocalVue, waitNT } from '@/../tests/utils' // should be the first to implement default mocks
import ForgotPassowrd from '../landing/components/ForgotPassword'
import { mount } from '@vue/test-utils'
import flushPromises from 'flush-promises'
import store from '@/store'
import router from '@/router'

const localVue = createAppLocalVue()

describe('Login - Landing - Forgot Password', () => {
  let wrapper, changePassword, sendResetLink, loadAllCountries

  beforeEach(async () => {
    changePassword = jest.fn().mockImplementation(() => Promise.resolve(true))
    sendResetLink = jest.fn(() => {})
    loadAllCountries = jest.fn()

    wrapper = mount(ForgotPassowrd, {
      localVue,
      store,
      router,
      methods: {
        changePassword,
        sendResetLink,
        loadAllCountries,
      },
      data: () => ({
        resetErrorStatus: null,
        resetErrorMessage: [],
      }),
      computed: {
        getResetErrorStatus() {
          return this.$data.resetErrorStatus
        },
        getResetErrorMessage() {
          return this.$data.resetErrorMessage
        },
        callCodeList: () => [{ value: 1, text: '+60' }],
      },
    })

    await flushPromises()
  })

  it('Should display', async () => {
    expect(wrapper.isVueInstance()).toBeTruthy()
  })

  it('Should display errors', async () => {
    wrapper.setData({
      resetErrorStatus: true,
      resetErrorMessage: ['error1'],
    })

    await waitNT(wrapper.vm)
    expect(wrapper.find('.alert').text()).toContain('error1')
  })

  describe('Selected Member', () => {
    beforeEach(async () => {
      wrapper
        .find({ ref: 'forgotPasswordType' })
        .findAll('input')
        .at(0)
        .trigger('click')

      await waitNT(wrapper.vm)
    })

    it('Should validate fields on submit', async () => {
      // change the language
      wrapper.find({ ref: 'submitButton' }).trigger('click')

      await waitNT(wrapper.vm)

      expect(wrapper.vm.$validator.errors.any()).toBe(true)
      expect(sendResetLink).not.toBeCalled()
    })

    it('Should validate fields and submit', async () => {
      // change the language
      wrapper.find({ ref: 'memberId' }).setValue('10000000007')
      wrapper.find({ ref: 'submitButton' }).trigger('click')

      await flushPromises()

      expect(wrapper.vm.$validator.errors.any()).toBe(false)
      expect(sendResetLink).toBeCalledWith({
        old_member_id: '10000000007',
      })
    })
  })

  describe('Selected Email', () => {
    beforeEach(async () => {
      wrapper
        .find({ ref: 'forgotPasswordType' })
        .findAll('input')
        .at(1)
        .trigger('click')

      await waitNT(wrapper.vm)
    })

    it('Should validate fields on submit', async () => {
      // change the language
      wrapper.find({ ref: 'submitButton' }).trigger('click')

      await waitNT(wrapper.vm)

      expect(wrapper.vm.$validator.errors.any()).toBe(true)
      expect(sendResetLink).not.toBeCalled()
    })

    it('Should validate fields and submit', async () => {
      // change the language
      wrapper.find({ ref: 'email' }).setValue('shiman.dc@gmail.com')
      wrapper.find({ ref: 'submitButton' }).trigger('click')

      await flushPromises()

      expect(wrapper.vm.$validator.errors.any()).toBe(false)
      expect(sendResetLink).toBeCalledWith({
        email: 'shiman.dc@gmail.com',
      })
    })
  })

  describe('Selected Mobile', () => {
    beforeEach(async () => {
      wrapper
        .find({ ref: 'forgotPasswordType' })
        .findAll('input')
        .at(2)
        .trigger('click')

      await waitNT(wrapper.vm)
    })

    it('Should validate fields on submit', async () => {
      // change the language
      wrapper.find({ ref: 'submitButton' }).trigger('click')

      await waitNT(wrapper.vm)

      expect(wrapper.vm.$validator.errors.any()).toBe(true)
      expect(sendResetLink).not.toBeCalled()
    })

    it('Should validate fields and submit', async () => {
      // change the language
      wrapper
        .find({ ref: 'mobileCountryCodeId' })
        .findAll('option')
        .at(1)
        .setSelected()
      wrapper.find({ ref: 'phoneNumber' }).setValue('12312312')

      await wrapper.vm.$nextTick()

      wrapper.find({ ref: 'submitButton' }).trigger('click')

      await flushPromises()

      expect(wrapper.vm.$validator.errors.any()).toBe(false)
      expect(sendResetLink).toBeCalledWith({
        mobile_country_code_id: 1,
        mobile_num: '12312312',
      })
    })
  })
})
