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(view): fix the forwardRef functionality for view #5

Open
wants to merge 2 commits 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
60 changes: 60 additions & 0 deletions __tests__/forwardRef.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React, { forwardRef, useRef } from 'react';
import { render, cleanup } from '@testing-library/react/pure';
// eslint-disable-next-line import/no-unresolved
import { view, store } from '@frontity/react-easy-state';
import { act } from 'react-dom/test-utils';

describe('forwardRef', () => {
afterEach(cleanup);

describe('function components', () => {
test('should forward ref', () => {
let usedRef;
const counter = store({ num: 0 });

const CompInnerForward = forwardRef((props, ref) => {
return (
<div ref={ref} {...props}>
{counter.num}
</div>
);
});

const MyOtherComp = view(CompInnerForward);

const App = () => {
const refone = useRef();
usedRef = refone;

return <MyOtherComp ref={refone} />;
};

act(() => {
render(<App />);
});

expect(usedRef.current).toMatchInlineSnapshot(`
<div>
0
</div>
`);
});

test('should re-render when store changes', () => {
let renderCount = 0;
const person = store({ name: 'Bob' });
const MyComp = view(
forwardRef(() => {
renderCount += 1;
return <div>{person.name}</div>;
}),
);
const { container } = render(<MyComp />);
expect(renderCount).toBe(1);
expect(container).toHaveTextContent('Bob');
person.name = 'Ann';
expect(renderCount).toBe(2);
expect(container).toHaveTextContent('Ann');
});
});
});
Loading