Skip to content

Hoc pattern, test fixes and clean-ups #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 8, 2018
Merged

Hoc pattern, test fixes and clean-ups #34

merged 6 commits into from
Aug 8, 2018

Conversation

hartzis
Copy link
Collaborator

@hartzis hartzis commented Aug 5, 2018

"Step 1" of #33

  • implement basic HOC pattern
    • fix tests
  • add tests for asyncScriptOnLoad
  • add tests for onload and onerror
  • pass script entry object to asyncScriptOnLoad
    • maybe remove observers key? just have loaded/errored and maybe add the url?

TODO:

  • update readme with changes

@dozoisch Let me know about the question above and if you have any other questions or updates.

also removed the old ie support check. it was actually broken anyway 😭
onreadystatechange has the wrong reference for this:

script.onreadystatechange = () => {
  if (this.readyState === "loaded") {

should be:

script.onreadystatechange = () => {
  if (script.readyState === "loaded") {

reference: https://www.html5rocks.com/en/tutorials/speed/script-loading/

With v1.0.0 release time to say goodbye to old ie<10?

);
assert.equal(hasScript(URL), true);

assert.equal(hasScript(URL), false, "Url not in document");
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this one was "wrong" before... it was checking the previous tests url instead of verifying that this tests url was not in the document


const documentErrorScript = (URL) => {
const script = getScript(URL);
script.onerror();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added some util functions for the new tests

const URL = "http://example.com";
const ComponentWrapper = makeAsyncScriptLoader(MockedComponent, URL);
it("should return a component that contains the passed component and fire asyncScriptOnLoad", () => {
const URL = "http://example.com/?default=true";
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

made each URL unique for each test. this helps prevent mix ups like the one cleaned up below 😺

const instance = ReactTestUtils.renderIntoDocument(
<ComponentWrapper />
);
assert.equal(hasScript(URL), true);
ReactDOM.unmountComponentAtNode(ReactDOM.findDOMNode(instance));
instance.componentWillUnmount();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added .parentNode to the unmountComponentAtNode call which now correctly removes the mounted component so the components componentWillUnmount is called correctly 😺

import PropTypes from "prop-types";

let SCRIPT_MAP = {};

// A counter used to generate a unique id for each component that uses the function
let idCount = 0;

export default function makeAsyncScript(Component, getScriptURL, options) {
export default function makeAsyncScript(getScriptURL, options) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The below diff looks intense, but mostly just indentation due to HOC 😢

biggest changes are:

  • the added (props, context) to the constructor
  • added comments to steps inside of componentDidMount
  • passing the entry object to asyncScriptOnLoad

Copy link
Owner

@dozoisch dozoisch Aug 6, 2018

Choose a reason for hiding this comment

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

can remove the whitespace from the diff => https://github.com/dozoisch/react-async-script/pull/34/files?w=1 . w=1 makes it easier to review in this case :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

^ that's awesome... i need to remember that for all time.. lol

@hartzis hartzis mentioned this pull request Aug 5, 2018
7 tasks
@dozoisch
Copy link
Owner

dozoisch commented Aug 6, 2018

Yeah I think we can remove the old IE check. Yeah it should've been an anonymous function instead of arrow lol.. guess it got linted at some point and I didn't notice.

@dozoisch
Copy link
Owner

dozoisch commented Aug 6, 2018

LGTM! I'll let you do the merge in case there is something else you want to do before merging :)

@hartzis
Copy link
Collaborator Author

hartzis commented Aug 6, 2018

@dozoisch yeah.. need to update the documentation 😸

thank you for the awesome and timely review!

@@ -69,8 +82,7 @@ You can still retrieve the child component using `getComponent()`.
### Example

```js
const MockedComponent = React.createClass({
Copy link
Owner

Choose a reason for hiding this comment

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

oh wow that was an old example X) Didn't realize I never update it to classes.

@hartzis hartzis mentioned this pull request Aug 8, 2018
@hartzis hartzis added this to the v1.0.0 milestone Aug 8, 2018
@dozoisch dozoisch merged commit 2143364 into master Aug 8, 2018
@dozoisch dozoisch deleted the hoc-pattern branch August 8, 2018 05:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants