|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { DOMWrapper, mount, VueWrapper } from '@vue/test-utils'; |
| 3 | +import { getReactiveForm, globalMountOptions } from '../helpers'; |
| 4 | +import FormQuestion from '@/components/FormQuestion.vue'; |
| 5 | +import RankControl from '@/components/controls/RankControl.vue'; |
| 6 | +import { RankNode } from '@getodk/xforms-engine'; |
| 7 | + |
| 8 | +describe('RankControl', () => { |
| 9 | + const getAllOptions = (rankControl: VueWrapper): string[] => { |
| 10 | + return rankControl.findAll('.rank-label').map((element) => element.text()); |
| 11 | + }; |
| 12 | + |
| 13 | + const getRankControl = async () => { |
| 14 | + const xform = await getReactiveForm('1-rank.xml'); |
| 15 | + |
| 16 | + const component = mount(FormQuestion, { |
| 17 | + props: { |
| 18 | + question: xform.currentState.children[0] as RankNode, |
| 19 | + }, |
| 20 | + global: globalMountOptions, |
| 21 | + }); |
| 22 | + |
| 23 | + return component.findComponent(RankControl); |
| 24 | + }; |
| 25 | + |
| 26 | + const swapItems = (options: string[], indexA: number, indexB: number) => { |
| 27 | + const temp = options[indexA]; |
| 28 | + options[indexA] = options[indexB]; |
| 29 | + options[indexB] = temp; |
| 30 | + }; |
| 31 | + |
| 32 | + const moveOptionUp = async (rankControl: VueWrapper, optionIndex: number) => { |
| 33 | + const buttonMoveUp: DOMWrapper<HTMLElement> = rankControl.find( |
| 34 | + `.rank-option:nth-child(${optionIndex}) button:first-child` |
| 35 | + ); |
| 36 | + expect(buttonMoveUp.exists()).toBe(true); |
| 37 | + await buttonMoveUp.trigger('click'); |
| 38 | + }; |
| 39 | + |
| 40 | + const moveOptionDown = async (rankControl: VueWrapper, optionIndex: number) => { |
| 41 | + const buttonMoveUp: DOMWrapper<HTMLElement> = rankControl.find( |
| 42 | + `.rank-option:nth-child(${optionIndex}) button:nth-child(2)` |
| 43 | + ); |
| 44 | + expect(buttonMoveUp.exists()).toBe(true); |
| 45 | + await buttonMoveUp.trigger('click'); |
| 46 | + }; |
| 47 | + |
| 48 | + it('should render all options', async () => { |
| 49 | + const expectedOptions = [ |
| 50 | + 'Career Growth and Learning Opportunities', |
| 51 | + 'Building a Supportive Community', |
| 52 | + 'Financial Stability', |
| 53 | + 'Pursuit of Hobbies and Passions', |
| 54 | + 'Health', |
| 55 | + 'Family and Friends', |
| 56 | + 'Personal Development and Mindfulness', |
| 57 | + 'Environmental Sustainability', |
| 58 | + 'Creativity and Innovation', |
| 59 | + 'Time Management and Work-Life Balance', |
| 60 | + ]; |
| 61 | + |
| 62 | + const rankControl = (await getRankControl()) as VueWrapper; |
| 63 | + expect(rankControl.exists()).toBe(true); |
| 64 | + |
| 65 | + const allOptions = getAllOptions(rankControl); |
| 66 | + expect(allOptions.length).toEqual(10); |
| 67 | + |
| 68 | + const allOptionsExist = expectedOptions.every((option) => allOptions.includes(option)); |
| 69 | + expect(allOptionsExist).toBe(true); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should rank options using buttons', async () => { |
| 73 | + const rankControl = (await getRankControl()) as VueWrapper; |
| 74 | + expect(rankControl.exists()).toBe(true); |
| 75 | + |
| 76 | + const expectedOptions = getAllOptions(rankControl); |
| 77 | + swapItems(expectedOptions, 4, 3); |
| 78 | + swapItems(expectedOptions, 6, 7); |
| 79 | + |
| 80 | + await moveOptionUp(rankControl, 5); |
| 81 | + await moveOptionDown(rankControl, 7); |
| 82 | + |
| 83 | + const allRankedOptions = getAllOptions(rankControl); |
| 84 | + const sameOrder = expectedOptions.every((value, index) => value === allRankedOptions[index]); |
| 85 | + expect(sameOrder).toBe(true); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should not move options if they are the first or last one', async () => { |
| 89 | + const rankControl = (await getRankControl()) as VueWrapper; |
| 90 | + expect(rankControl.exists()).toBe(true); |
| 91 | + |
| 92 | + const expectedOptions = getAllOptions(rankControl); |
| 93 | + swapItems(expectedOptions, 3, 2); |
| 94 | + |
| 95 | + await moveOptionUp(rankControl, 1); |
| 96 | + await moveOptionUp(rankControl, 4); |
| 97 | + await moveOptionDown(rankControl, 10); |
| 98 | + |
| 99 | + const allRankedOptions = getAllOptions(rankControl); |
| 100 | + const sameOrder = expectedOptions.every((value, index) => value === allRankedOptions[index]); |
| 101 | + expect(sameOrder).toBe(true); |
| 102 | + }); |
| 103 | +}); |
0 commit comments