<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ES6 &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/es6/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 16:27:39 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>ES6 &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>React + Recompose: Calling Multiple HOC Wrappers</title>
		<link>https://myshittycode.com/2018/04/02/react-recompose-calling-multiple-hoc-wrappers/</link>
					<comments>https://myshittycode.com/2018/04/02/react-recompose-calling-multiple-hoc-wrappers/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Mon, 02 Apr 2018 17:37:35 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[ES6]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[React]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=1081</guid>

					<description><![CDATA[<p>PROBLEM Sometimes, wrapping a React component with multiple High Order Components (HOC) can get rather unwieldy and unreadable. For example:- SOLUTION To fix this, we can leverage recompose library. Now, we can rewrite the above example like this:- Keep in mind, the HOC order defined in compose(..) is important.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2018/04/02/react-recompose-calling-multiple-hoc-wrappers/">React + Recompose: Calling Multiple HOC Wrappers</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>Sometimes, wrapping a React component with multiple High Order Components (HOC) can get rather unwieldy and unreadable.</p>



<p>For example:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; highlight: [10]; title: ; notranslate">
import React from &#039;react&#039;;
import { withRouter } from &#039;react-router-dom&#039;;
import { withStyles } from &#039;material-ui/styles&#039;;
import withWidth from &#039;material-ui/utils/withWidth&#039;;

class MyComponent extends React.PureComponent {
	// ...
}

export default withRouter(withStyles(styles)(withWidth()(MyComponent)));
</pre></div>


<h2 class="wp-block-heading">SOLUTION</h2>



<p>To fix this, we can leverage <a href="https://github.com/acdlite/recompose" target="_blank" rel="noopener">recompose</a> library.</p>



<p>Now, we can rewrite the above example like this:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; highlight: [5,11,12,13,14,15]; title: ; notranslate">
import React from &#039;react&#039;;
import { withRouter } from &#039;react-router-dom&#039;;
import { withStyles } from &#039;material-ui/styles&#039;;
import withWidth from &#039;material-ui/utils/withWidth&#039;;
import compose from &#039;recompose/compose&#039;;

class MyComponent extends React.PureComponent {
	// ...
}

export default compose(
  withRouter,
  withStyles(styles),
  withWidth(),
)(MyComponent);
</pre></div>


<p>Keep in mind, the HOC order defined in <strong>compose(..)</strong> is important.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2018/04/02/react-recompose-calling-multiple-hoc-wrappers/">React + Recompose: Calling Multiple HOC Wrappers</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2018/04/02/react-recompose-calling-multiple-hoc-wrappers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1081</post-id>	</item>
		<item>
		<title>ES6 + Mocha + Sinon: Mocking Imported Dependency</title>
		<link>https://myshittycode.com/2017/05/10/es6-mocha-sinon-mocking-imported-dependency/</link>
					<comments>https://myshittycode.com/2017/05/10/es6-mocha-sinon-mocking-imported-dependency/#comments</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Wed, 10 May 2017 20:46:35 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[ES6]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Mocha]]></category>
		<category><![CDATA[Sinon]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=1014</guid>

					<description><![CDATA[<p>PROBLEM Let&#8217;s assume we have the following 2 files:- apis.js service.js Let&#8217;s assume we want to test the logic in service.js without using nock to mock the HTTP call in apis.js. While proxyquireify allows us to mock out the apis.js dependency in service.js, sometimes it is a little more complicated than needed. SOLUTION A simpler [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2017/05/10/es6-mocha-sinon-mocking-imported-dependency/">ES6 + Mocha + Sinon: Mocking Imported Dependency</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>Let&#8217;s assume we have the following 2 files:-</p>



<p><b><b>apis.js</b></b></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
import fetch from &#039;isomorphic-fetch&#039;;

export const logout = () =&gt; (
  fetch(&#039;/logout&#039;)
    .then(resp =&gt; resp.json())
    .catch(err =&gt; err)
);
</pre></div>


<p><b><b>service.js</b></b></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
import { logout } from &#039;./apis&#039;;

export const kickUserOut = activeSession =&gt; (
  activeSession ? logout() : undefined
);
</pre></div>


<p>Let&#8217;s assume we want to test the logic in <b>service.js</b> without using <a href="https://github.com/node-nock/nock" target="_blank" rel="noopener">nock</a> to mock the HTTP call in <b>apis.js</b>.</p>



<p>While <a href="https://github.com/thlorenz/proxyquireify" target="_blank" rel="noopener">proxyquireify</a> allows us to mock out the <b>apis.js</b> dependency in <b>service.js</b>, sometimes it is a little more complicated than needed.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>A simpler approach is to use <a href="https://github.com/sinonjs/sinon" target="_blank" rel="noopener">sinon</a> to stub out <b>logout()</b> defined in <b>apis.js</b>.</p>



<p><b><b>service-spec.js</b></b></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
import { beforeEach, afterEach, describe, it } from &#039;mocha&#039;;
import { expect } from &#039;chai&#039;;
import sinon from &#039;sinon&#039;;
import { kickUserOut } from &#039;./service&#039;;

// import everything as an object
import * as apis from &#039;./apis&#039;;

describe(&#039;service =&gt; kickUserOut&#039;, () =&gt; {
  let logoutStub;

  // before running each test, stub out `logout()`
  beforeEach(() =&gt; {
    logoutStub = sinon.stub(apis, &#039;logout&#039;).returns(&#039;success&#039;);
  });

  // after running each test, restore to the original method to
  // prevent &quot;TypeError: Attempted to wrap logout which is already wrapped&quot;
  // error when executing subsequent specs.
  afterEach(() =&gt; {
    apis.logout.restore();
  });

  it(&#039;given active session, should invoke logout API&#039;, () =&gt; {
    expect(kickUserOut(true)).to.deep.equal(&#039;success&#039;);
    expect(logoutStub.calledOnce).to.equal(true);
  });

  it(&#039;given expired session, should not invoke logout API&#039;, () =&gt; {
    expect(kickUserOut(false)).to.equal(undefined);
    expect(logoutStub.calledOnce).to.equal(false);
  });
});
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2017/05/10/es6-mocha-sinon-mocking-imported-dependency/">ES6 + Mocha + Sinon: Mocking Imported Dependency</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2017/05/10/es6-mocha-sinon-mocking-imported-dependency/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1014</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching 55/67 queries in 0.038 seconds using Disk

Served from: myshittycode.com @ 2026-02-20 16:44:09 by W3 Total Cache
-->