Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

fix(holocronModule): remove check for initial-state #56

Merged
merged 2 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 7 additions & 40 deletions packages/holocron/__tests__/holocronModule.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ describe('holocronModule', () => {
});

it('should dispatch the module\'s load action on componentDidMount with no preloaded state', () => {
global.__INITIAL_STATE__ = undefined;
const load = jest.fn(() => () => Promise.resolve());
const Module = holocronModule({
name: 'mock-module',
Expand All @@ -239,37 +238,6 @@ describe('holocronModule', () => {
expect(load).toHaveBeenCalledTimes(1);
});

it('should not dispatch the module\'s load action on client takeover of a server render', () => {
global.__INITIAL_STATE__ = { some: 'state' };
const load = jest.fn(() => () => Promise.resolve());
const Module = holocronModule({
name: 'mock-module',
load,
})(() => <div>Mock Module</div>);
const mockStore = createStore((state) => state, applyMiddleware(thunk));
renderer.create(
<Provider store={mockStore}>
<Module />
</Provider>
);
expect(load).not.toHaveBeenCalled();
});

it('should not dispatch the module\'s load action on the server render', () => {
const load = jest.fn();
const Module = holocronModule({
name: 'mock-module',
load,
})(({ moduleLoadStatus }) => <div>Mock Module - {moduleLoadStatus}</div>);
const mockStore = createStore((state) => state);
renderer.create(
<Provider store={mockStore}>
<Module />
</Provider>
);
expect(load).not.toHaveBeenCalled();
});

it('should dispatch the module\'s load action if it receives new props that pass shouldModuleReload', () => {
const load = jest.fn(() => () => Promise.resolve());
const Module = connect((state) => state)(holocronModule({
Expand All @@ -287,12 +255,13 @@ describe('holocronModule', () => {
<Module />
</Provider>
);
expect(load).toHaveBeenCalledTimes(1);
mockStore.dispatch({ type: 'MOCK_ACTION_TYPE', newState: { someParam: 'new' } });
// couldn't use toHaveBeenCalledWith because mapDispatchToProps is used
const calledProps = load.mock.calls[0][0];
const calledProps = load.mock.calls[1][0];
const calledPropsWithoutFunctions = _.pickBy(calledProps, (p) => typeof p !== 'function');
expect(calledPropsWithoutFunctions).toEqual({ someParam: 'new' });
expect(load).toHaveBeenCalledTimes(1);
expect(load).toHaveBeenCalledTimes(2);
});

it('should not dispatch the module\'s load action if it receives new props that do not pass shouldModuleReload', () => {
Expand All @@ -308,9 +277,10 @@ describe('holocronModule', () => {
<Module />
</Provider>
);
expect(load).toHaveBeenCalledTimes(1);
const { dispatch } = mockStore;
dispatch({ type: 'MOCK_ACTION_TYPE', newState: { someParam: 'initial', differentParam: 'new' } });
expect(load).not.toHaveBeenCalled();
expect(load).toHaveBeenCalledTimes(1);
});

it('should not dispatch the module\'s load action if no shouldModuleReload function is provided', () => {
Expand All @@ -325,14 +295,14 @@ describe('holocronModule', () => {
<Module />
</Provider>
);
expect(load).toHaveBeenCalledTimes(1);
const { dispatch } = mockStore;
dispatch({ type: 'MOCK_ACTION_TYPE', newState: { someParam: 'new' } });
expect(load).not.toHaveBeenCalled();
expect(load).toHaveBeenCalledTimes(1);
});

// TODO: use enzyme to assert correct props, need to update version of jest first
it('should pass the moduleLoadStatus prop as loading when loading', () => {
global.__INITIAL_STATE__ = undefined;
const loadPromise = Promise.resolve();
const load = jest.fn(() => () => loadPromise);
const Module = holocronModule({
Expand All @@ -351,7 +321,6 @@ describe('holocronModule', () => {

// TODO: use enzyme to assert correct props, need to update version of jest first
it('should pass the moduleLoadStatus prop as loaded when loaded', async () => {
global.__INITIAL_STATE__ = undefined;
const loadPromise = Promise.resolve();
const load = jest.fn(() => () => loadPromise);
const Module = holocronModule({
Expand All @@ -378,7 +347,6 @@ describe('holocronModule', () => {

// TODO: use enzyme to assert correct props, need to update version of jest first
it('should pass the moduleLoadStatus prop as error when it failed to load', () => {
global.__INITIAL_STATE__ = undefined;
const loadPromise = Promise.reject();
const load = jest.fn(() => () => loadPromise);
const Module = holocronModule({
Expand All @@ -403,7 +371,6 @@ describe('holocronModule', () => {
});

it('should gracefully handle load not returning a Promise', () => {
global.__INITIAL_STATE__ = undefined;
const load = jest.fn(() => () => 'not a promise');
const Module = holocronModule({
name: 'mock-module',
Expand Down
2 changes: 1 addition & 1 deletion packages/holocron/src/holocronModule.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export default function holocronModule({
componentDidMount() {
this.mounted = true;
// eslint-disable-next-line no-underscore-dangle
JAdshead marked this conversation as resolved.
Show resolved Hide resolved
if ((loadModuleData || load) && !global.__INITIAL_STATE__) {
if (loadModuleData || load) {
this.initiateLoad(0, this.props);
}
}
Expand Down