diff --git a/src/lib/components/radio-group/RadioGroup.svelte b/src/lib/components/radio-group/RadioGroup.svelte index df11010..f4758ec 100644 --- a/src/lib/components/radio-group/RadioGroup.svelte +++ b/src/lib/components/radio-group/RadioGroup.svelte @@ -1,7 +1,7 @@ - active = e.detail}> + Pizza Delivery {#if showFirst} Pickup @@ -139,14 +140,14 @@ describe('Rendering', () => { }) it('should be possible to disable a RadioGroup', async () => { - let changeFn = jest.fn() + let value = writable(null); render(svelte` - + Pizza Delivery Pickup Home delivery @@ -158,7 +159,7 @@ describe('Rendering', () => { `) - // Try to click one a few options + // Try to click on a few options await click(getByText('Pickup')) await click(getByText('Dine in')) @@ -171,8 +172,8 @@ describe('Rendering', () => { }) ) - // Make sure that the onChange handler never got called - expect(changeFn).toHaveBeenCalledTimes(0) + // Make sure that the value has not changed + expect(get(value)).toBeNull(); // Make sure that all the options get an `aria-disabled` let options = getRadioGroupOptions() @@ -194,19 +195,19 @@ describe('Rendering', () => { // Try to click one a few options await click(getByText('Pickup')) - // Make sure that the onChange handler got called - expect(changeFn).toHaveBeenCalledTimes(1) + // Make sure that the value changed + expect(get(value)).toEqual("pickup"); }) it('should be possible to disable a RadioGroupOption', async () => { - let changeFn = jest.fn() + let value = writable(null); render(svelte` - + Pizza Delivery Pickup Home delivery @@ -229,8 +230,8 @@ describe('Rendering', () => { }) ) - // Make sure that the onChange handler never got called - expect(changeFn).toHaveBeenCalledTimes(0) + // Make sure that the value has not changed + expect(get(value)).toBeNull(); // Make sure that the option with value "slot-prop" gets an `aria-disabled` let options = getRadioGroupOptions() @@ -259,7 +260,7 @@ describe('Rendering', () => { await click(document.querySelector('[data-value="slot-prop"]')) // Make sure that the onChange handler got called - expect(changeFn).toHaveBeenCalledTimes(1) + expect(get(value)).toEqual("slot-prop"); }) }) @@ -268,7 +269,7 @@ describe('Keyboard interactions', () => { describe('`Tab` key', () => { it('should be possible to tab to the first item', async () => { render(svelte` - + Pizza Delivery Pickup Home delivery @@ -283,8 +284,11 @@ describe('Keyboard interactions', () => { it('should not change the selected element on focus', async () => { let changeFn = jest.fn() + let value = writable(null); + value.subscribe(changeFn); + render(svelte` - + Pizza Delivery Pickup Home delivery @@ -296,12 +300,13 @@ describe('Keyboard interactions', () => { assertActiveElement(getByText('Pickup')) - expect(changeFn).toHaveBeenCalledTimes(0) + // 1 for initial subscription + expect(changeFn).toHaveBeenCalledTimes(1) }) it('should be possible to tab to the active item', async () => { render(svelte` - + Pizza Delivery Pickup Home delivery @@ -316,8 +321,11 @@ describe('Keyboard interactions', () => { it('should not change the selected element on focus (when selecting the active item)', async () => { let changeFn = jest.fn() + let value = writable("home-delivery"); + value.subscribe(changeFn); + render(svelte` - + Pizza Delivery Pickup Home delivery @@ -329,13 +337,14 @@ describe('Keyboard interactions', () => { assertActiveElement(getByText('Home delivery')) - expect(changeFn).toHaveBeenCalledTimes(0) + // 1 for initial subscription + expect(changeFn).toHaveBeenCalledTimes(1) }) it('should be possible to tab out of the radio group (no selected value)', async () => { render(svelte` - + Pizza Delivery Pickup Home delivery @@ -357,7 +366,7 @@ describe('Keyboard interactions', () => { it('should be possible to tab out of the radio group (selected value)', async () => { render(svelte` - + Pizza Delivery Pickup Home delivery @@ -381,7 +390,7 @@ describe('Keyboard interactions', () => { describe('`Shift+Tab` key', () => { it('should be possible to tab to the first item', async () => { render(svelte` - + Pizza Delivery Pickup Home delivery @@ -400,8 +409,11 @@ describe('Keyboard interactions', () => { it('should not change the selected element on focus', async () => { let changeFn = jest.fn() + let value = writable(null); + value.subscribe(changeFn); + render(svelte` - + Pizza Delivery Pickup Home delivery @@ -417,12 +429,13 @@ describe('Keyboard interactions', () => { assertActiveElement(getByText('Pickup')) - expect(changeFn).toHaveBeenCalledTimes(0) + // 1 for initial subscription + expect(changeFn).toHaveBeenCalledTimes(1) }) it('should be possible to tab to the active item', async () => { render(svelte` - + Pizza Delivery Pickup Home delivery @@ -441,8 +454,11 @@ describe('Keyboard interactions', () => { it('should not change the selected element on focus (when selecting the active item)', async () => { let changeFn = jest.fn() + let value = writable("home-delivery"); + value.subscribe(changeFn); + render(svelte` - + Pizza Delivery Pickup Home delivery @@ -457,13 +473,14 @@ describe('Keyboard interactions', () => { assertActiveElement(getByText('Home delivery')) - expect(changeFn).toHaveBeenCalledTimes(0) + // 1 for initial subscription + expect(changeFn).toHaveBeenCalledTimes(1) }) it('should be possible to tab out of the radio group (no selected value)', async () => { render(svelte` - + Pizza Delivery Pickup Home delivery @@ -485,7 +502,7 @@ describe('Keyboard interactions', () => { it('should be possible to tab out of the radio group (selected value)', async () => { render(svelte` - + Pizza Delivery Pickup Home delivery @@ -508,9 +525,11 @@ describe('Keyboard interactions', () => { describe('`ArrowLeft` key', () => { it('should go to the previous item when pressing the ArrowLeft key', async () => { let changeFn = jest.fn() + let value = writable(null); + value.subscribe(changeFn); render(svelte` - changeFn(e.detail)}> + Pizza Delivery Pickup Home delivery @@ -534,18 +553,21 @@ describe('Keyboard interactions', () => { await press(Keys.ArrowLeft) assertActiveElement(getByText('Home delivery')) - expect(changeFn).toHaveBeenCalledTimes(2) - expect(changeFn).toHaveBeenNthCalledWith(1, 'dine-in') - expect(changeFn).toHaveBeenNthCalledWith(2, 'home-delivery') + // 1 for initial subscription + expect(changeFn).toHaveBeenCalledTimes(3) + expect(changeFn).toHaveBeenNthCalledWith(2, 'dine-in') + expect(changeFn).toHaveBeenNthCalledWith(3, 'home-delivery') }) }) describe('`ArrowUp` key', () => { it('should go to the previous item when pressing the ArrowUp key', async () => { let changeFn = jest.fn() + let value = writable(null); + value.subscribe(changeFn); render(svelte` - changeFn(e.detail)}> + Pizza Delivery Pickup Home delivery @@ -569,18 +591,21 @@ describe('Keyboard interactions', () => { await press(Keys.ArrowUp) assertActiveElement(getByText('Home delivery')) - expect(changeFn).toHaveBeenCalledTimes(2) - expect(changeFn).toHaveBeenNthCalledWith(1, 'dine-in') - expect(changeFn).toHaveBeenNthCalledWith(2, 'home-delivery') + // 1 for initial subscription + expect(changeFn).toHaveBeenCalledTimes(3) + expect(changeFn).toHaveBeenNthCalledWith(2, 'dine-in') + expect(changeFn).toHaveBeenNthCalledWith(3, 'home-delivery') }) }) describe('`ArrowRight` key', () => { it('should go to the next item when pressing the ArrowRight key', async () => { let changeFn = jest.fn() + let value = writable(null); + value.subscribe(changeFn); render(svelte` - changeFn(e.detail)}> + Pizza Delivery Pickup Home delivery @@ -607,19 +632,22 @@ describe('Keyboard interactions', () => { await press(Keys.ArrowRight) // Loop around assertActiveElement(getByText('Pickup')) - expect(changeFn).toHaveBeenCalledTimes(3) - expect(changeFn).toHaveBeenNthCalledWith(1, 'home-delivery') - expect(changeFn).toHaveBeenNthCalledWith(2, 'dine-in') - expect(changeFn).toHaveBeenNthCalledWith(3, 'pickup') + // 1 for initial subscription + expect(changeFn).toHaveBeenCalledTimes(4) + expect(changeFn).toHaveBeenNthCalledWith(2, 'home-delivery') + expect(changeFn).toHaveBeenNthCalledWith(3, 'dine-in') + expect(changeFn).toHaveBeenNthCalledWith(4, 'pickup') }) }) describe('`ArrowDown` key', () => { it('should go to the next item when pressing the ArrowDown key', async () => { let changeFn = jest.fn() + let value = writable(null); + value.subscribe(changeFn); render(svelte` - changeFn(e.detail)}> + Pizza Delivery Pickup Home delivery @@ -646,19 +674,23 @@ describe('Keyboard interactions', () => { await press(Keys.ArrowDown) // Loop around assertActiveElement(getByText('Pickup')) - expect(changeFn).toHaveBeenCalledTimes(3) - expect(changeFn).toHaveBeenNthCalledWith(1, 'home-delivery') - expect(changeFn).toHaveBeenNthCalledWith(2, 'dine-in') - expect(changeFn).toHaveBeenNthCalledWith(3, 'pickup') + // 1 for initial subscription + expect(changeFn).toHaveBeenCalledTimes(4) + expect(changeFn).toHaveBeenNthCalledWith(2, 'home-delivery') + expect(changeFn).toHaveBeenNthCalledWith(3, 'dine-in') + expect(changeFn).toHaveBeenNthCalledWith(4, 'pickup') }) }) describe('`Space` key', () => { it('should select the current option when pressing space', async () => { let changeFn = jest.fn() + let value = writable(null); + value.subscribe(changeFn); + render(svelte` - changeFn(e.detail)}> + Pizza Delivery Pickup Home delivery @@ -679,20 +711,19 @@ describe('Keyboard interactions', () => { await press(Keys.Space) assertActiveElement(getByText('Pickup')) - expect(changeFn).toHaveBeenCalledTimes(1) - expect(changeFn).toHaveBeenNthCalledWith(1, 'pickup') + // 1 for initial subscription + expect(changeFn).toHaveBeenCalledTimes(2) + expect(changeFn).toHaveBeenNthCalledWith(2, 'pickup') }) it('should select the current option only once when pressing space', async () => { let changeFn = jest.fn() + let value = writable(null); + value.subscribe(changeFn); render(svelte` - - - { value = e.detail; changeFn(e.detail)} }> + Pizza Delivery Pickup Home delivery @@ -716,8 +747,9 @@ describe('Keyboard interactions', () => { await press(Keys.Space) assertActiveElement(getByText('Pickup')) - expect(changeFn).toHaveBeenCalledTimes(1) - expect(changeFn).toHaveBeenNthCalledWith(1, 'pickup') + // 1 for initial subscription + expect(changeFn).toHaveBeenCalledTimes(2) + expect(changeFn).toHaveBeenNthCalledWith(2, 'pickup') }) }) }) @@ -725,9 +757,12 @@ describe('Keyboard interactions', () => { describe('Mouse interactions', () => { it('should be possible to change the current radio group value when clicking on a radio option', async () => { let changeFn = jest.fn() + let value = writable(null); + value.subscribe(changeFn); + render(svelte` - changeFn(e.detail)}> + Pizza Delivery Pickup Home delivery @@ -740,19 +775,17 @@ describe('Mouse interactions', () => { assertActiveElement(getByText('Home delivery')) - expect(changeFn).toHaveBeenNthCalledWith(1, 'home-delivery') + expect(changeFn).toHaveBeenNthCalledWith(2, 'home-delivery') }) it('should be a no-op when clicking on the same item', async () => { let changeFn = jest.fn() + let value = writable(null); + value.subscribe(changeFn); render(svelte` - - - { value = e.detail; changeFn(e.detail)} }> + Pizza Delivery Pickup Home delivery @@ -768,6 +801,7 @@ describe('Mouse interactions', () => { assertActiveElement(getByText('Home delivery')) - expect(changeFn).toHaveBeenCalledTimes(1) + // 1 for initial subscription + expect(changeFn).toHaveBeenCalledTimes(2) }) })