Skip to content

Commit

Permalink
Add runtime test validating plural @LiVe field
Browse files Browse the repository at this point in the history
Reviewed By: voideanvalue

Differential Revision: D45677696

fbshipit-source-id: 12b16097e1be9fb62dd1167dd4ef8aae0cdbfd8c
  • Loading branch information
captbaritone authored and facebook-github-bot committed May 9, 2023
1 parent 6347254 commit 6a80a0f
Show file tree
Hide file tree
Showing 5 changed files with 400 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,26 @@ describe.each([
});
}

function ManyLiveTodosComponent() {
const data = useClientQuery(
graphql`
query RelayResolversWithOutputTypeTestManyLiveTodosQuery {
many_live_todos {
...RelayResolversWithOutputTypeTestFragment
}
}
`,
{},
);
if (data.many_live_todos?.length === 0) {
return 'No Items';
}

return data.many_live_todos?.map((todo, index) => {
return <TodoComponent key={index} fragmentKey={todo} />;
});
}

test('should render empty state', () => {
const renderer = TestRenderer.create(
<EnvironmentWrapper environment={environment}>
Expand Down Expand Up @@ -718,4 +738,44 @@ describe.each([
'color: color is red',
]);
});

test('rendering live list', () => {
addTodo('Todo 1');
addTodo('Todo 2');
addTodo('Todo 3');

const renderer = TestRenderer.create(
<EnvironmentWrapper environment={environment}>
<ManyLiveTodosComponent />
</EnvironmentWrapper>,
);

expect(renderer.toJSON()).toEqual([
'Todo 1',
'is not completed',
'style: bold',
'color: color is red',
'Todo 2',
'is not completed',
'style: bold',
'color: color is red',
'Todo 3',
'is not completed',
'style: bold',
'color: color is red',
]);

TestRenderer.act(() => {
removeTodo('todo-1');
removeTodo('todo-2');
jest.runAllImmediates();
});

expect(renderer.toJSON()).toEqual([
'Todo 3',
'is not completed',
'style: bold',
'color: color is red',
]);
});
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import type {LogEvent} from '../../RelayStoreTypes';

export opaque type TodoID = string;
export opaque type TodoID: string = string;

export type TodoItem = {
todoID: TodoID,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall relay
*/

'use strict';

import type {LiveState} from '../../experimental-live-resolvers/LiveResolverStore';

const {
Selectors,
TODO_STORE,
} = require('relay-runtime/store/__tests__/resolvers/ExampleTodoStore');

/**
* @RelayResolver Query.many_live_todos: [Todo]
* @live
*/
function many_live_todos(): LiveState<$ReadOnlyArray<{todo_id: string}>> {
return {
read() {
return Selectors.getTodoIDs(TODO_STORE.getState()).map(id => ({
todo_id: id,
}));
},
subscribe(cb) {
return TODO_STORE.subscribe(null, cb);
},
};
}

module.exports = {
many_live_todos,
};
Loading

0 comments on commit 6a80a0f

Please sign in to comment.