Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #139 #141

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/components/Shuttle/Shuttle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,15 @@ export const Shuttle: React.FC<ShuttleProps> & Statics = ({
children,
...rest
}: ShuttleProps) => {
const counter = React.useRef(0);
const { onClick: defaultClickHandler } = useShuttleItemClick({ setShuttleState, shuttleState });

return (
<ShuttleContext.Provider value={{ shuttleState, setShuttleState }}>
<ShuttleContext.Provider
value={React.useMemo(
() => ({ shuttleState, setShuttleState, counter }),
[shuttleState, setShuttleState, counter]
)}>
<div
className={classNames(
'shuttle',
Expand Down
6 changes: 2 additions & 4 deletions src/components/Shuttle/ShuttleContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { ShuttleContext } from './ShuttleContext';
import { NUMBER_OF_CONTAINERS, SHUTTLE_CONTAINERS_ARRAY } from './globals';
import { ShuttleState } from './hooks/useShuttleState';

let id_int = 0;

export type ShuttleContainerProps = {
/**
* Child render function of the Shuttle Container.
Expand Down Expand Up @@ -34,11 +32,11 @@ export const ShuttleContainer: React.FC<ShuttleContainerProps> = React.memo(
// eslint-disable-next-line react/display-name
React.forwardRef<HTMLDivElement, ShuttleContainerProps>(
({ children, className, ...rest }, ref) => {
const { shuttleState } = React.useContext(ShuttleContext);
const { shuttleState, counter } = React.useContext(ShuttleContext);

// mod needed for HMR updates
const id = React.useRef(
SHUTTLE_CONTAINERS_ARRAY[Math.floor(id_int++ % NUMBER_OF_CONTAINERS)]
SHUTTLE_CONTAINERS_ARRAY[Math.floor(counter.current++ % NUMBER_OF_CONTAINERS)]
);

/**
Expand Down
1 change: 1 addition & 0 deletions src/components/Shuttle/ShuttleContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ShuttleState } from './hooks/useShuttleState';
type ShuttleContextType = {
shuttleState: ShuttleState;
setShuttleState: React.Dispatch<Record<string, any>>;
counter: React.MutableRefObject<number>;
};

// @ts-ignore
Expand Down
22 changes: 8 additions & 14 deletions src/components/Shuttle/__tests__/ShuttleContainer.test.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
import * as React from 'react';
import { render, cleanup } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';

import React from 'react';
import { render, cleanup, screen } from '@testing-library/react';
import { ShuttleContainer } from '../ShuttleContainer';

afterEach(cleanup);

describe('ShuttleContainer tests', () => {
it('should render', () => {
expect(() => {
render(<ShuttleContainer>{() => <p>hello</p>}</ShuttleContainer>);
}).not.toThrow();
});

it('should contain data-name and other essential attributes', () => {
const { container, getByTestId } = render(
jest.spyOn(React, 'useContext').mockReturnValue({ counter: { current: 1 } });

const { container } = render(
<ShuttleContainer data-testid="container">{() => <p>hello</p>}</ShuttleContainer>
);

expect(getByTestId('container')).toHaveAttribute('data-name', 'target');
expect(getByTestId('container')).toHaveAttribute('role', 'listbox');
expect(getByTestId('container')).toHaveAttribute('tabindex', '0');
expect(screen.getByTestId('container')).toHaveAttribute('data-name', 'target');
expect(screen.getByTestId('container')).toHaveAttribute('role', 'listbox');
expect(screen.getByTestId('container')).toHaveAttribute('tabindex', '0');

expect(container).toMatchSnapshot();
});
Expand Down