import Vue from 'vue'
import { mount } from '@vue/test-utils'
import faker from 'faker'

import configStore from '../mocks/config-store'
import storeMock from '../mocks/store-mock'

import Customers from '@/views/Customers.vue'

const mockCustomers = []

for (let i = 0; i < 10; i++) {
  mockCustomers.push({
    id: faker.random.number(),
    contact: faker.name.findName(),
    email: faker.internet.email(),
    address: faker.address.streetAddress(),
    state: faker.address.state(),
    phone: faker.phone.phoneNumber()
  })
}

Vue.prototype.$auth = {
  user: jest.fn(() => ({
    name
  }))
}

jest.doMock('axios', () => {
  return {
    get: jest.fn(() => Promise.resolve({
      status: 200,
      data: {
        data: mockCustomers
      }
    }))
  }
})

storeMock.state = {
  user: {
    name: faker.name.findName(),
    view_permissions: 'Admin'
  }
}

storeMock.modules.Customers.actions = {
  deleteCustomer: jest.fn(),
  deleteCustomers: jest.fn(),
  loadCustomers: jest.fn(),
  loadCatalog: jest.fn(),
  actionCustomer: jest.fn(),
  setModalVisible: jest.fn(),
  printReport: jest.fn(),
  applyFilter: jest.fn(),
  clearFilter: jest.fn()
}

storeMock.modules.Customers.state.customers = mockCustomers

describe('Customers.vue', () => {
  const store = configStore(Vue, storeMock)
  beforeEach(() => {
    jest.resetModules()
    jest.clearAllMocks()
  })

  it('should render correct contents', (done) => {
    const wrapper = mount(Customers, {
      store,
      mocks: {
        $route: {
          params: { id: '' }
        }
      }
    })

    setTimeout(() => {
      expect(wrapper.findAll('.tr-table')).toHaveLength(mockCustomers.length)
      expect(wrapper.findAll('.vm-table--td span').at(0).text()).toEqual(mockCustomers[0].contact)

      done()
    }, 1500)
  })
})
