Skip to content
This repository has been archived by the owner on Nov 27, 2022. It is now read-only.

Commit

Permalink
Allow subscribing to an array of resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenVerborgh committed Mar 7, 2019
1 parent 7c8f237 commit d72b162
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
no-multi-assign: off,
no-multiple-empty-lines: error,
no-negated-condition: off,
no-nested-ternary: error,
no-nested-ternary: off,
no-new-object: error,
no-plusplus: off,
no-restricted-syntax: error,
Expand Down
5 changes: 3 additions & 2 deletions src/components/LiveUpdate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ const { Provider } = UpdateContext;

/**
* Component that creates an UpdateContext by subscribing
* to updates of a whitespace-separated list of resources.
* to updates of an array (or whitespace-separated string) of resources.
*
* Children or descendants that use UpdateContext as a context
* will be rerendered if any of those resources are updated.
*/
export default function LiveUpdate({ subscribe = '', children = null }) {
const urls = subscribe.trim() ? subscribe.split(/\s+/) : [];
const urls = typeof subscribe !== 'string' ? subscribe :
(/\S/.test(subscribe) ? subscribe.trim().split(/\s+/) : []);
const latestUpdate = useLatestUpdate(...urls);
return <Provider value={latestUpdate}>{children}</Provider>;
}
20 changes: 18 additions & 2 deletions test/components/LiveUpdate-test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ describe('a LiveUpdate component', () => {
});
});

describe('subscribing to two resources', () => {
describe('subscribing to two resources with an array', () => {
beforeAll(() => {
useLatestUpdate.mockClear();
({ container } = render(
<LiveUpdate subscribe="https://a.com/1 https://b.com/2">
<LiveUpdate subscribe={['https://a.com/1', 'https://b.com/2']}>
<ShowContext/>
</LiveUpdate>
));
Expand All @@ -73,4 +73,20 @@ describe('a LiveUpdate component', () => {
expect(container.innerHTML).toBe('{"update":true}');
});
});

describe('subscribing to two resources with a string', () => {
beforeAll(() => {
useLatestUpdate.mockClear();
({ container } = render(
<LiveUpdate subscribe=" https://a.com/1 https://b.com/2 ">
<ShowContext/>
</LiveUpdate>
));
});

it('calls useLatestUpdate with the given resources', () => {
expect(useLatestUpdate).toHaveBeenCalledTimes(1);
expect(useLatestUpdate).toHaveBeenCalledWith('https://a.com/1', 'https://b.com/2');
});
});
});

0 comments on commit d72b162

Please sign in to comment.