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

test(refs): added tests for ref utility #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions src/__tests__/refs.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import Brahmos, { render } from '..';
import { forwardRef, createRef, setRef } from '../refs';

describe('Test all ref utils work', () => {
it('should forward ref to a Component', () => {
class Component extends Brahmos.Component {
render () {
return null;
}
}

const ForwardedComponent = forwardRef((props, ref) => (
<Component ref={ref} />
));

const componentInstance = render(
<ForwardedComponent ref={111} />,
document.createElement('div')
);

expect(componentInstance.__nodes.ref).toBe(111);
});

it('should return a ref when createRef is called', () => {
const receivedRef = createRef();
expect(receivedRef).toEqual({
current: null,
});
});

it('should set instance as current when setRef called with object type ref', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setRef is inner API we should do integration test of it. Similar to what you did for forwardRefs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

const ref = createRef();
const instance = 'instance';
setRef(ref, instance);
expect(ref.current).toBe('instance');
});

it('should pass instance as param when setRef called with function type ref', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^^

const mockRef = jest.fn();
const instance = 'instance';
setRef(mockRef, instance);
expect(mockRef.mock.calls.length).toBe(1);
expect(mockRef.mock.calls[0][0]).toBe('instance');
});
});