Skip to content

Commit

Permalink
Created local ExecutionEnvironment with isServer (#2960)
Browse files Browse the repository at this point in the history
Summary:
This PR resolves the issue #2954 and fixes jest.mock.
Pull Request resolved: #2960

Reviewed By: kassens

Differential Revision: D19071027

Pulled By: jstejada

fbshipit-source-id: 3d5e216b23f5fe170ec75856d3bea484a77c1d37
  • Loading branch information
morrys authored and Juan Tejada committed Dec 19, 2019
1 parent 0a884d9 commit 7e850bc
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 31 deletions.
20 changes: 20 additions & 0 deletions packages/relay-experimental/ExecutionEnvironment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails oncall+relay
* @flow strict-local
* @format
*/

// flowlint ambiguous-object-type:error

'use strict';

const ExecutionEnvironment = {
isServer: typeof window === 'undefined',
};

module.exports = ExecutionEnvironment;
4 changes: 2 additions & 2 deletions packages/relay-experimental/QueryResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

'use strict';

const ExecutionEnvironment = require('fbjs/lib/ExecutionEnvironment');
const ExecutionEnvironment = require('./ExecutionEnvironment');
const LRUCache = require('./LRUCache');

const invariant = require('invariant');
Expand Down Expand Up @@ -152,7 +152,7 @@ function createCacheEntry(
temporaryRetain(environment: IEnvironment): Disposable {
// NOTE: If we're executing in a server environment, there's no need
// to create temporary retains, since the component will never commit.
if (!ExecutionEnvironment.canUseDOM) {
if (ExecutionEnvironment.isServer) {
return {dispose: () => {}};
}

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

'use strict';

jest.mock('fbjs/lib/ExecutionEnvironment', () => ({
canUseDOM: true,
jest.mock('../ExecutionEnvironment', () => ({
isServer: false,
}));

const EntryPointContainer = require('../EntryPointContainer.react');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

'use strict';

jest.mock('fbjs/lib/ExecutionEnvironment', () => ({
canUseDOM: true,
jest.mock('../ExecutionEnvironment', () => ({
isServer: false,
}));

const LazyLoadEntryPointContainer_DEPRECATED = require('../LazyLoadEntryPointContainer_DEPRECATED.react');
Expand Down
4 changes: 2 additions & 2 deletions packages/relay-experimental/__tests__/QueryResource-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ describe('QueryResource', () => {
};

beforeEach(() => {
jest.mock('fbjs/lib/ExecutionEnvironment', () => ({
canUseDOM: () => true,
jest.mock('../ExecutionEnvironment', () => ({
isServer: false,
}));
store = new Store(new RecordSource());
environment = createMockEnvironment({store});
Expand Down
4 changes: 2 additions & 2 deletions packages/relay-experimental/__tests__/fetchQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ describe('fetchQuery', () => {
let retained = [];
beforeEach(() => {
retained = [];
jest.mock('fbjs/lib/ExecutionEnvironment', () => ({
canUseDOM: () => true,
jest.mock('../ExecutionEnvironment', () => ({
isServer: false,
}));
environment = createMockEnvironment();
environment.retain.mockImplementation(obj => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

'use strict';

jest.mock('fbjs/lib/ExecutionEnvironment', () => ({
canUseDOM: true,
jest.mock('../ExecutionEnvironment', () => ({
isServer: false,
}));

const preloadQuery_DEPRECATED = require('../preloadQuery_DEPRECATED');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

'use strict';

jest.mock('fbjs/lib/ExecutionEnvironment', () => ({
canUseDOM: true,
jest.mock('../ExecutionEnvironment', () => ({
isServer: false,
}));

const prepareEntryPoint = require('../prepareEntryPoint');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ describe('useBlockingPaginationFragment', () => {
jest.resetModules();
jest.spyOn(console, 'warn').mockImplementationOnce(() => {});
jest.mock('warning');
jest.mock('fbjs/lib/ExecutionEnvironment', () => ({
canUseDOM: () => true,
jest.mock('../ExecutionEnvironment', () => ({
isServer: false,
}));
renderSpy = jest.fn();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ describe('useBlockingPaginationFragment with useSuspenseTransition', () => {
jest.resetModules();
jest.spyOn(console, 'warn').mockImplementationOnce(() => {});
jest.mock('warning');
jest.mock('fbjs/lib/ExecutionEnvironment', () => ({
canUseDOM: () => true,
jest.mock('../ExecutionEnvironment', () => ({
isServer: false,
}));
renderSpy = jest.fn();

Expand Down
4 changes: 2 additions & 2 deletions packages/relay-experimental/__tests__/useFragmentNode-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ beforeEach(() => {
jest.mock('scheduler', () => {
return jest.requireActual('scheduler/unstable_mock');
});
jest.mock('fbjs/lib/ExecutionEnvironment', () => ({
canUseDOM: () => true,
jest.mock('../ExecutionEnvironment', () => ({
isServer: false,
}));
renderSpy = jest.fn();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ describe('useLazyLoadQueryNode', () => {
beforeEach(() => {
jest.resetModules();
jest.spyOn(console, 'warn').mockImplementationOnce(() => {});
jest.mock('fbjs/lib/ExecutionEnvironment', () => ({
canUseDOM: () => true,
jest.mock('../ExecutionEnvironment', () => ({
isServer: false,
}));

({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -663,8 +663,8 @@ describe('useLegacyPaginationFragment', () => {

beforeEach(() => {
jest.resetModules();
jest.mock('fbjs/lib/ExecutionEnvironment', () => ({
canUseDOM: () => true,
jest.mock('../ExecutionEnvironment', () => ({
isServer: false,
}));
jest.doMock('scheduler', () => {
const original = jest.requireActual('scheduler/unstable_mock');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

'use strict';

jest.mock('fbjs/lib/ExecutionEnvironment', () => ({
canUseDOM: true,
jest.mock('../ExecutionEnvironment', () => ({
isServer: false,
}));

const React = require('react');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ describe('useRefetchableFragmentNode', () => {
jest.mock('scheduler', () => {
return jest.requireActual('scheduler/unstable_mock');
});
jest.mock('fbjs/lib/ExecutionEnvironment', () => ({
canUseDOM: () => true,
jest.mock('../ExecutionEnvironment', () => ({
isServer: false,
}));
renderSpy = jest.fn();

Expand Down
6 changes: 3 additions & 3 deletions packages/relay-experimental/preloadQuery_DEPRECATED.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

'use strict';

const ExecutionEnvironment = require('fbjs/lib/ExecutionEnvironment');
const ExecutionEnvironment = require('./ExecutionEnvironment');

const invariant = require('invariant');

Expand Down Expand Up @@ -159,7 +159,7 @@ function preloadQueryDeduped<TQuery: OperationType>(
kind: 'cache',
name: params.name,
};
if (ExecutionEnvironment.canUseDOM && prevQueryEntry == null) {
if (!ExecutionEnvironment.isServer && prevQueryEntry == null) {
setTimeout(() => {
// Clear the cache entry after the default timeout
// null-check for Flow
Expand Down Expand Up @@ -191,7 +191,7 @@ function preloadQueryDeduped<TQuery: OperationType>(
subject,
subscription: source
.finally(() => {
if (!ExecutionEnvironment.canUseDOM) {
if (ExecutionEnvironment.isServer) {
return;
}
setTimeout(() => {
Expand Down

0 comments on commit 7e850bc

Please sign in to comment.