import { describe, expect, test, vi } from 'vitest'
import type { Query as PiniaOrmQuery } from 'pinia-orm'
import type ApiResource from '~/models/ApiResource'
import OrderBy from '~/services/data/Filters/OrderBy'
import { ORDER_BY_DIRECTION } from '~/types/enum/data'

describe('constructor', () => {
  test('simple call', () => {
    const filter = new OrderBy('foo')

    expect(filter.field).toEqual('foo')
  })

  test('simple call with desc direction', () => {
    const filter = new OrderBy('foo', ORDER_BY_DIRECTION.DESC)

    expect(filter.field).toEqual('foo')
    expect(filter.mode).toEqual('desc')
  })
})

describe('applyToPiniaOrmQuery', () => {
  test('simple call', () => {
    const filter = new OrderBy('foo')

    // @ts-ignore
    const piniaOrmQuery1 = vi.fn() as PiniaOrmQuery<ApiResource>

    // @ts-ignore
    const piniaOrmQuery2 = vi.fn() as PiniaOrmQuery<ApiResource>

    piniaOrmQuery1.orderBy = vi.fn(() => piniaOrmQuery2)

    const result = filter.applyToPiniaOrmQuery(piniaOrmQuery1)

    expect(result).toEqual(piniaOrmQuery2)
  })
})

describe('getApiQueryPart', () => {
  test('simple call', () => {
    const filter = new OrderBy('foo', ORDER_BY_DIRECTION.ASC)

    const result = filter.getApiQueryPart()

    expect(result).toEqual('order[foo]=asc')
  })

  test('with descendent direction', () => {
    const filter = new OrderBy('foo', ORDER_BY_DIRECTION.DESC)

    const result = filter.getApiQueryPart()

    expect(result).toEqual('order[foo]=desc')
  })
})
