From d107e409fe6e6037d04dd84690c0ef3d9ca54043 Mon Sep 17 00:00:00 2001 From: aabramov Date: Tue, 18 Jul 2023 12:34:13 +0300 Subject: [PATCH] refactor: Disable templates dropdown when templates array is empty --- .../TemplateSelect/TemplateSelect.test.js | 17 +++++++++++++++++ src/Components/TemplateSelect/index.js | 3 ++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Components/TemplateSelect/TemplateSelect.test.js b/src/Components/TemplateSelect/TemplateSelect.test.js index 19986b29..3a13d8b4 100644 --- a/src/Components/TemplateSelect/TemplateSelect.test.js +++ b/src/Components/TemplateSelect/TemplateSelect.test.js @@ -3,6 +3,7 @@ import TemplateSelect from '.'; import userEvent from '@testing-library/user-event'; import { render, screen } from '../../mocks/utils'; import { templates } from '../../mocks/fixtures/templates.fixtures'; +import { provisioningUrl } from '../../API/helpers'; describe('TemplateSelect', () => { test('get all templates options', async () => { @@ -23,4 +24,20 @@ describe('TemplateSelect', () => { const placeholder = await screen.findByText('Select templates'); expect(placeholder).toBeInTheDocument(); }); + + test('when templates array is empty it should be disabled', async () => { + const { server, rest } = window.msw; + const chosenSource = '1'; + server.use( + rest.get(provisioningUrl(`sources/${chosenSource}/launch_templates`), (req, res, ctx) => { + return res(ctx.status(200), ctx.json([])); + }) + ); + render(, { provider: 'aws', contextValues: { chosenSource: chosenSource } }); + const selectDropdown = await screen.findByText('No template found'); + await userEvent.click(selectDropdown); + + const placeholder = await screen.findByText('No template found'); + expect(placeholder).toBeInTheDocument(); + }); }); diff --git a/src/Components/TemplateSelect/index.js b/src/Components/TemplateSelect/index.js index e5c752dc..885f5469 100644 --- a/src/Components/TemplateSelect/index.js +++ b/src/Components/TemplateSelect/index.js @@ -51,10 +51,11 @@ const TemplatesSelect = () => { onToggle={(openState) => setIsOpen(openState)} selections={chosenTemplateName} onSelect={onSelect} - placeholderText="Select templates" + placeholderText={templates?.length === 0 ? 'No template found' : 'Select templates'} aria-label="Select templates" clearSelectionsAriaLabel="clear template selection" onClear={onClear} + isDisabled={templates?.length === 0} > {templates && selectItemsMapper()}