From 79dde0b722ce910f906a3b17163771df06a05395 Mon Sep 17 00:00:00 2001 From: Elizabeth Thompson Date: Thu, 14 Oct 2021 17:11:24 -0700 Subject: [PATCH 1/3] update values for default timezone selector --- .../TimezoneSelector.test.tsx | 29 ++++++++++++++-- .../src/components/TimezoneSelector/index.tsx | 33 +++++++++++-------- 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/superset-frontend/src/components/TimezoneSelector/TimezoneSelector.test.tsx b/superset-frontend/src/components/TimezoneSelector/TimezoneSelector.test.tsx index f0b12d4777537..5d393f811eb69 100644 --- a/superset-frontend/src/components/TimezoneSelector/TimezoneSelector.test.tsx +++ b/superset-frontend/src/components/TimezoneSelector/TimezoneSelector.test.tsx @@ -18,14 +18,18 @@ */ import React from 'react'; import moment from 'moment-timezone'; -import { render } from 'spec/helpers/testing-library'; +import { render, screen } from 'spec/helpers/testing-library'; +import userEvent from '@testing-library/user-event'; import TimezoneSelector from './index'; describe('TimezoneSelector', () => { - let timezone: string; + let timezone: string | undefined; const onTimezoneChange = jest.fn(zone => { timezone = zone; }); + beforeEach(() => { + timezone = undefined; + }); it('renders a TimezoneSelector with a default if undefined', () => { jest.spyOn(moment.tz, 'guess').mockReturnValue('America/New_York'); render( @@ -36,6 +40,27 @@ describe('TimezoneSelector', () => { ); expect(onTimezoneChange).toHaveBeenCalledWith('America/Nassau'); }); + it('should properly select values from the offsetsToName map', async () => { + jest.spyOn(moment.tz, 'guess').mockReturnValue('America/New_York'); + render( + , + ); + + const select = screen.getByRole('combobox', { + name: 'Timezone Selector', + }); + expect(select).toBeInTheDocument(); + userEvent.click(select); + const selection = await screen.findByTitle( + 'GMT -06:00 (Mountain Daylight Time)', + ); + expect(selection).toBeInTheDocument(); + userEvent.click(selection); + expect(selection).toBeVisible(); + }); it('renders a TimezoneSelector with the closest value if passed in', async () => { render( { const prevTimezone = useRef(timezone); const matchTimezoneToOptions = (timezone: string) => TIMEZONE_OPTIONS.find(option => option.offsets === getOffsetKey(timezone)) - ?.value || DEFAULT_TIMEZONE; + ?.value || DEFAULT_TIMEZONE.value; - const updateTimezone = (tz: string) => { - // update the ref to track changes - prevTimezone.current = tz; - // the parent component contains the state for the value - onTimezoneChange(tz); - }; + const updateTimezone = useCallback( + (tz: string) => { + // update the ref to track changes + prevTimezone.current = tz; + // the parent component contains the state for the value + onTimezoneChange(tz); + }, + [onTimezoneChange], + ); useEffect(() => { const updatedTz = matchTimezoneToOptions(timezone || moment.tz.guess()); if (prevTimezone.current !== updatedTz) { updateTimezone(updatedTz); } - }, [timezone]); + }, [timezone, updateTimezone]); return ( Date: Fri, 15 Oct 2021 18:40:48 -0700 Subject: [PATCH 3/3] Update TimezoneSelector.test.tsx --- .../src/components/TimezoneSelector/TimezoneSelector.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/superset-frontend/src/components/TimezoneSelector/TimezoneSelector.test.tsx b/superset-frontend/src/components/TimezoneSelector/TimezoneSelector.test.tsx index 5d393f811eb69..faa38b02b1ebe 100644 --- a/superset-frontend/src/components/TimezoneSelector/TimezoneSelector.test.tsx +++ b/superset-frontend/src/components/TimezoneSelector/TimezoneSelector.test.tsx @@ -50,7 +50,7 @@ describe('TimezoneSelector', () => { ); const select = screen.getByRole('combobox', { - name: 'Timezone Selector', + name: 'Timezone selector', }); expect(select).toBeInTheDocument(); userEvent.click(select);