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

Fixes #9. #12

Merged
merged 5 commits into from
Jun 28, 2018
Merged

Fixes #9. #12

merged 5 commits into from
Jun 28, 2018

Conversation

Ethorsen
Copy link
Contributor

  • Add a mapExtraState option to subspaced enhancer.
  • Allow instance enhancers
Q A
Fixed Issues? Fixes #9
Documentation only PR
Patch: Bug Fix?
Minor: New Feature? 👍
Major: Breaking Change?
Tests Added + Pass? No

- Add a mapExtraState option to subspaced enhancer.
- Allow instance enhancers
const mapState = (state, rootState) => {
const componentState = state[identifier]
if (!isPlainObject(componentState)) {
// perhaps a warning/error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should make a decision one way or the other on these warnings. I'm thinking that this one is ok, as the slice state might legitimately not be a object, but the extraState should always return an object (otherwise how should it be merged?

if (!isPlainObject(extraState)) {
  if (process.env.NODE_ENV !== 'production' && extraState !== null) {
    console.warn(`extra state must be a plain object but received ${extraState}`)
  }
  return componentState
}

I could be convinced that it should throw a TypeError instead of just warn. @jpeyper?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know I'll gladly adapt the PR

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd go with a TypeError (because it is a type error and you've used it wrong... Bang!) in dev and "just work" in production.

@mpeyper
Copy link
Contributor

mpeyper commented Jun 26, 2018

I think the inclusion in instance enhancers is good, but I'm a little worried that some users may get confused and think that they are able to override the existing enhancers, similarly to have the identifier is replaced and not added to the existing identifier (maybe it should concat them? I'll think about that a bit...).

Any chance of some unit tests for subspaced with a mapExtraState option provided and a createInstance with some extra enhancers?

Can I also ask a quick (mostly) unrelated question. I proposed a change to the API to allow identifiers, and possibly enhancer options as props to the dynamic component instead of having a createInstance function in #10. Do you think that would work for you, or do you like/prefer having the createInstance function?

@Ethorsen
Copy link
Contributor Author

Ethorsen commented Jun 27, 2018

I think the inclusion in instance enhancers is good, but I'm a little worried that some users may get confused and think that they are able to override the existing enhancers, similarly to have the identifier is replaced and not added to the existing identifier (maybe it should concat them? I'll think about that a bit...).

I was thinking the same, but as they are parameters array I would not know how to de-duplicate/overwrite enhancers of the same type. I also did not want to make a code breaking change.

Any chance of some unit tests for subspaced with a mapExtraState option provided and a createInstance with some extra enhancers?

I'll have a look tomorrow and will try to create and commit tests

Can I also ask a quick (mostly) unrelated question. I proposed a change to the API to allow identifiers, and possibly enhancer options as props to the dynamic component instead of having a createInstance function in #10. Do you think that would work for you, or do you like/prefer having the createInstance function?

I believe that would also work, plus I would not have to keep a reference to the instantiated module.

Added some tests for new functionalities
}))(ConnectedTestComponent)

// Silence error thrown by react
console.error = () => {}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nasty hack but I haven't found a way to silence react error thrown to console. Even adding and error boundary object did not help.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One man's hack is another man's hammer: Exhibit A, B and C

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that this will ever reset console.error for other tests (possibly hiding useful debugging information)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exhibit B caught my eye

@mpeyper
Copy link
Contributor

mpeyper commented Jun 28, 2018

Please also don't forget to add yourself as a contributor

@@ -0,0 +1,99 @@
/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests should be in the subspaced.test.js file. You can put them in a nested describe('mapExtraState tests', ...) if you want to seperate them from the other tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok


const fakeStore = {}

const testEnhancer1 = identifier => store => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary but you could avoid duplicating the enhancer code by making this

const testEnhancer = instance => identifier => store => {
  expect(identifier).toBe('testId')
  expect(store).toBe(fakeStore)
  callback(instance)
}

and then calling them like

const DynamicComponent = dynamic('baseId', testEnhancer(1))(TestComponent).createInstance('testId', testEnhancer(2))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

import configureStore from 'redux-mock-store'
import { Provider, connect } from 'react-redux'
import { SubspaceProvider } from 'react-redux-subspace'
import { mount } from 'enzyme'

import dynostore from '../../redux-dynostore-core/lib/index'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was about to merge this then noticed this in my final scan. This is a problematic import for 2 reasons:

  1. it is pathing outside of the package into another - this is brittle if the other package ever gets refactored
  2. it's referencing the bundled output, which means the other package would need to be built before this test would work - it's purely coincidental that that occurs as part of bootstrapping lerna for us.

in this case, importing it from the core package as you would any other npm dependency is all that is needed. lerna takes care of the rest for us:

import dynostore from '@redux-dynostore/core'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Sry I missed it, sometimes IDEA does some weird import when you cut/paste code.

@mpeyper
Copy link
Contributor

mpeyper commented Jun 28, 2018

Thanks!

As a follow up PR (for you or anyone else), a small example demonstrating a use case for this would be very welcome.

I might wait for #13 before doing a release, but if it takes more than a few days I'll push this out by itself.

@mpeyper mpeyper merged commit 503e4ce into ioof-holdings:master Jun 28, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants