Skip to content

Commit

Permalink
calling viewer handler multiple times should not accidentally cause v…
Browse files Browse the repository at this point in the history
…iewer to be null

Reviewed By: JenniferWang

Differential Revision: D6015530

fbshipit-source-id: 217d3ad825f75ff872e9b965c733792ce6a7d003
  • Loading branch information
yuzhi authored and facebook-github-bot committed Oct 11, 2017
1 parent 6f3581e commit f442fcd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 27 deletions.
3 changes: 2 additions & 1 deletion packages/relay-runtime/handlers/viewer/RelayViewerHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ function update(store: RecordSourceProxy, payload: HandleFieldPayload): void {
}
const serverViewer = record.getLinkedRecord(payload.fieldKey);
if (!serverViewer) {
record.setValue(null, payload.handleKey);
// If `serverViewer` is null, `viewer` key for `client:root` should already
// be null, so no need to `setValue` again.
return;
}
// Server data already has viewer data at `client:root:viewer`, so link the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,9 @@ describe('RelayViewerHandler', () => {
expect(sinkData).toEqual({});
});

it('sets the handle as deleted if the server viewer is null', () => {
baseSource.delete(VIEWER_ID);
RelayModernRecord.setLinkedRecordID(
baseSource.get(ROOT_ID),
'viewer',
VIEWER_ID,
);

const payload = {
dataID: ROOT_ID,
fieldKey: 'viewer',
handleKey: getRelayHandleKey('viewer', null, 'viewer'),
};
RelayViewerHandler.update(store, payload);
expect(sinkData).toEqual({
[ROOT_ID]: {
[ID_KEY]: ROOT_ID,
[TYPENAME_KEY]: ROOT_TYPE,
[payload.handleKey]: null,
},
});
});

it('sets the handle as deleted if the server viewer is undefined', () => {
it('links the handle to the server viewer for query data', () => {
const viewer = RelayModernRecord.create(VIEWER_ID, 'Viewer');
baseSource.set(VIEWER_ID, viewer);
RelayModernRecord.setLinkedRecordID(
baseSource.get(ROOT_ID),
'viewer',
Expand All @@ -100,12 +79,13 @@ describe('RelayViewerHandler', () => {
[ROOT_ID]: {
[ID_KEY]: ROOT_ID,
[TYPENAME_KEY]: ROOT_TYPE,
[payload.handleKey]: null,
viewer: null,
[payload.handleKey]: {[REF_KEY]: VIEWER_ID},
},
});
});

it('links the handle to the server viewer for query data', () => {
it('links the handle to the server viewer when called multiple times', () => {
const viewer = RelayModernRecord.create(VIEWER_ID, 'Viewer');
baseSource.set(VIEWER_ID, viewer);
RelayModernRecord.setLinkedRecordID(
Expand All @@ -119,6 +99,8 @@ describe('RelayViewerHandler', () => {
fieldKey: 'viewer',
handleKey: getRelayHandleKey('viewer', null, 'viewer'),
};
// called twice should not break
RelayViewerHandler.update(store, payload);
RelayViewerHandler.update(store, payload);
expect(sinkData).toEqual({
[ROOT_ID]: {
Expand Down

0 comments on commit f442fcd

Please sign in to comment.