Skip to content

Commit

Permalink
fix unit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
prabhuignoto committed Feb 13, 2024
1 parent 0478013 commit d213561
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 40 deletions.
100 changes: 63 additions & 37 deletions src/components/timeline/__tests__/layout-switcher.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fireEvent, render, waitFor } from '@testing-library/react';
import { getDefaultThemeOrDark } from '@utils/index';
import { getDefaultButtonTexts, getDefaultThemeOrDark } from '@utils/index';
import GlobalContextProvider from 'src/components/GlobalContext';
import { vi } from 'vitest';
import { LayoutSwitcher, QuickJump } from '../timeline-popover-elements';

Expand All @@ -11,13 +12,18 @@ describe('LayoutSwitcher Component', () => {

test('Renders correctly with vertical layout options', () => {
const { getByText } = render(
<LayoutSwitcher
onUpdateTimelineMode={onUpdateTimelineMode}
<GlobalContextProvider
theme={theme}
mode="VERTICAL"
isDarkMode={isDarkMode}
position={position}
/>,
buttonTexts={getDefaultButtonTexts()}
>
<LayoutSwitcher
onUpdateTimelineMode={onUpdateTimelineMode}
theme={theme}
mode="VERTICAL"
isDarkMode={isDarkMode}
position={position}
/>
</GlobalContextProvider>,
);

// Add your assertions here
Expand All @@ -31,13 +37,18 @@ describe('LayoutSwitcher Component', () => {

test('Renders correctly with horizontal layout options', () => {
const { getByText } = render(
<LayoutSwitcher
onUpdateTimelineMode={onUpdateTimelineMode}
<GlobalContextProvider
theme={theme}
mode="HORIZONTAL"
isDarkMode={isDarkMode}
position={position}
/>,
buttonTexts={getDefaultButtonTexts()}
>
<LayoutSwitcher
onUpdateTimelineMode={onUpdateTimelineMode}
theme={theme}
mode="HORIZONTAL"
isDarkMode={isDarkMode}
position={position}
/>
</GlobalContextProvider>,
);

// Add your assertions here
Expand All @@ -51,13 +62,18 @@ describe('LayoutSwitcher Component', () => {

test('Handles mode selection correctly', async () => {
const { getByText } = render(
<LayoutSwitcher
onUpdateTimelineMode={onUpdateTimelineMode}
<GlobalContextProvider
theme={theme}
mode="VERTICAL"
isDarkMode={isDarkMode}
position={position}
/>,
buttonTexts={getDefaultButtonTexts()}
>
<LayoutSwitcher
onUpdateTimelineMode={onUpdateTimelineMode}
theme={theme}
mode="VERTICAL"
isDarkMode={isDarkMode}
position={position}
/>
</GlobalContextProvider>,
);

expect(getByText('Change layout')).toBeInTheDocument();
Expand Down Expand Up @@ -87,41 +103,51 @@ describe('QuickJump Component', () => {

test('Renders correctly with provided items', () => {
const { getByText } = render(
<QuickJump
activeItem={0}
items={items}
<GlobalContextProvider
theme={theme}
onActivateItem={onActivateItem}
isDarkMode={isDarkMode}
position={position}
/>,
buttonTexts={getDefaultButtonTexts()}
>
<QuickJump
activeItem={0}
items={items}
theme={theme}
onActivateItem={onActivateItem}
isDarkMode={isDarkMode}
position={position}
/>
</GlobalContextProvider>,
);

// Add your assertions here

expect(getByText('Jump to a date')).toBeInTheDocument();
expect(getByText('Jump to')).toBeInTheDocument();

fireEvent.click(getByText('Jump to a date'));
fireEvent.click(getByText('Jump to'));

expect(getByText('Item 1')).toBeInTheDocument();
expect(getByText('Item 2')).toBeInTheDocument();
});

test('Handles item activation correctly', () => {
const { getByText } = render(
<QuickJump
activeItem={0}
items={items}
<GlobalContextProvider
theme={theme}
onActivateItem={onActivateItem}
isDarkMode={isDarkMode}
position={position}
/>,
buttonTexts={getDefaultButtonTexts()}
>
<QuickJump
activeItem={0}
items={items}
theme={theme}
onActivateItem={onActivateItem}
isDarkMode={isDarkMode}
position={position}
/>
</GlobalContextProvider>,
);

expect(getByText('Jump to a date')).toBeInTheDocument();
expect(getByText('Jump to')).toBeInTheDocument();

fireEvent.click(getByText('Jump to a date'));
fireEvent.click(getByText('Jump to'));

// Simulate clicking on an item
fireEvent.click(getByText('Item 1'));
Expand Down
7 changes: 4 additions & 3 deletions src/components/timeline/timeline-popover-elements.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const LayoutSwitcher: FunctionComponent<LayoutSwitcherProp> = ({
isDarkMode,
position,
}: LayoutSwitcherProp) => {
const { showAllCardsHorizontal } = useContext(GlobalContext);
const { showAllCardsHorizontal, buttonTexts } = useContext(GlobalContext);

const activeTimelineMode = useMemo(
() => mode,
Expand Down Expand Up @@ -81,7 +81,7 @@ const LayoutSwitcher: FunctionComponent<LayoutSwitcherProp> = ({

return (
<PopOver
placeholder="Change layout"
placeholder={buttonTexts.changeLayout}
position={position}
theme={theme}
isDarkMode={isDarkMode}
Expand All @@ -107,9 +107,10 @@ const QuickJump: FunctionComponent<QuickJumpProp> = ({
isDarkMode,
position,
}: QuickJumpProp) => {
const {buttonTexts} = useContext(GlobalContext);
return (
<PopOver
placeholder="Jump to a date"
placeholder={buttonTexts.jumpTo}
position={position}
theme={theme}
width={'400px'}
Expand Down
2 changes: 2 additions & 0 deletions src/models/TimelineModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ export type TimelineProps = {

// custom button texts
buttonTexts?: {
changeLayout?: string;
dark?: string;
first: string;
jumpTo?: string;
last: string;
light?: string;
next?: string;
Expand Down
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ export const getDefaultClassNames = () => ({
});

export const getDefaultButtonTexts = () => ({
changeLayout: 'Change layout',
dark: 'Switch to Dark Mode',
first: 'Go to First',
jumpTo: 'Jump to',
last: 'Go to Last',
light: 'Switch to Light Mode',
next: 'Next',
Expand Down
2 changes: 2 additions & 0 deletions src/utils/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ describe('Utility Functions', () => {

it('getDefaultButtonTexts should return default button texts', () => {
expect(getDefaultButtonTexts()).toEqual({
changeLayout: 'Change layout',
dark: 'Switch to Dark Mode',
first: 'Go to First',
jumpTo: 'Jump to',
last: 'Go to Last',
light: 'Switch to Light Mode',
next: 'Next',
Expand Down

0 comments on commit d213561

Please sign in to comment.