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 Users from '@/views/Users.vue'

const mockUsers = []

for (let i = 0; i < 10; i++) {
  mockUsers.push({
    id: faker.random.number(),
    name: faker.name.firstName(),
    email: faker.internet.email(),
    view_permissions: [
      'Admin'
    ],
    status: 'Activo'
  })
}

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

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

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

storeMock.modules.Users.actions = {
  deleteRecord: jest.fn(),
  deleteRecords: jest.fn(),
  loadRecords: jest.fn(),
  loadCatalog: jest.fn(),
  actionRecord: jest.fn(),
  setModalVisible: jest.fn(),
  printReport: jest.fn(),
  applyFilter: jest.fn(),
  clearFilter: jest.fn()
}

storeMock.modules.Users.state.users = mockUsers

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

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

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

      done()
    }, 1500)
  })
})
