File size: 3,774 Bytes
81f4ed8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | import { describe, it, expect, vi } from 'vitest'
import { mount } from '@vue/test-utils'
import ButtonMapSendRequest from '@/components/buttons/ButtonMapSendRequest.vue'
/**
* The `map` prop expects a Leaflet LMap instance.
* We pass a plain object since the component only forwards it to sendMLRequest β
* no Leaflet methods are called directly by this component.
*/
const mockMap = {} as any
const samplePrompt = { id: 1, type: 'point', data: { lat: 45, lng: 9 }, label: 1 }
const baseProps = {
class: 'test-class',
currentBaseMapName: 'OpenStreetMap',
promptsArray: [samplePrompt],
responseMessage: '-',
map: mockMap,
sendMLRequest: vi.fn(),
waitingString: 'waiting...',
}
describe('ButtonMapSendRequest', () => {
// ββ State 1: empty prompt ββββββββββββββββββββββββββββββ
it('shows disabled button with empty prompt text when promptsArray is empty', () => {
const wrapper = mount(ButtonMapSendRequest, {
props: { ...baseProps, promptsArray: [] },
})
const button = wrapper.find('button')
expect(button.text()).toContain('Empty prompt (disabled)')
expect(button.attributes('disabled')).toBeDefined()
})
// ββ State 2: waiting βββββββββββββββββββββββββββββββββββ
it('shows disabled button with waitingString when responseMessage equals waitingString', () => {
const wrapper = mount(ButtonMapSendRequest, {
props: { ...baseProps, responseMessage: 'waiting...' },
})
const button = wrapper.find('button')
expect(button.text()).toBe('waiting...')
expect(button.attributes('disabled')).toBeDefined()
})
it('waiting state takes priority over empty prompt when both conditions true', () => {
const wrapper = mount(ButtonMapSendRequest, {
props: { ...baseProps, promptsArray: [], responseMessage: 'waiting...' },
})
expect(wrapper.find('button').text()).toBe('waiting...')
})
// ββ State 3: ready (default) βββββββββββββββββββββββββββ
it('shows send ML request text when prompts present and responseMessage is "-"', () => {
const wrapper = mount(ButtonMapSendRequest, { props: baseProps })
expect(wrapper.find('button').text()).toContain('send ML request')
})
it('shows send ML request text when prompts present and responseMessage is empty', () => {
const wrapper = mount(ButtonMapSendRequest, {
props: { ...baseProps, responseMessage: '' },
})
expect(wrapper.find('button').text()).toContain('send ML request')
})
it('enabled button is not disabled', () => {
const wrapper = mount(ButtonMapSendRequest, { props: baseProps })
expect(wrapper.find('button').attributes('disabled')).toBeUndefined()
})
// ββ State 4: has response message ββββββββββββββββββββββ
it('shows responseMessage when truthy and not "-"', () => {
const wrapper = mount(ButtonMapSendRequest, {
props: { ...baseProps, responseMessage: 'error: timeout' },
})
expect(wrapper.find('button').text()).toBe('error: timeout')
})
// ββ Click behavior βββββββββββββββββββββββββββββββββββββ
it('calls sendMLRequest with map, promptsArray, currentBaseMapName on click', async () => {
const sendFn = vi.fn()
const wrapper = mount(ButtonMapSendRequest, {
props: { ...baseProps, sendMLRequest: sendFn },
})
await wrapper.find('button').trigger('click')
expect(sendFn).toHaveBeenCalledOnce()
expect(sendFn).toHaveBeenCalledWith(mockMap, [samplePrompt], 'OpenStreetMap')
})
})
|