<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Blair Garrett on Medium]]></title>
        <description><![CDATA[Stories by Blair Garrett on Medium]]></description>
        <link>https://medium.com/@blairgarrett?source=rss-d9c383a6f02------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*k7PcT0ycbIVvMn2sY0vCDA@2x.jpeg</url>
            <title>Stories by Blair Garrett on Medium</title>
            <link>https://medium.com/@blairgarrett?source=rss-d9c383a6f02------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 05:15:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@blairgarrett/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to mock event.preventDefault() with Jest]]></title>
            <link>https://medium.com/@blairgarrett/how-to-mock-event-preventdefault-with-jest-9fa11f1d355e?source=rss-d9c383a6f02------2</link>
            <guid isPermaLink="false">https://medium.com/p/9fa11f1d355e</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[jest]]></category>
            <dc:creator><![CDATA[Blair Garrett]]></dc:creator>
            <pubDate>Sat, 23 Dec 2017 00:17:35 GMT</pubDate>
            <atom:updated>2017-12-23T00:17:35.285Z</atom:updated>
            <content:encoded><![CDATA[<p>**Level — Intermediate. Read Time ~ &lt; 3 minutes.**</p><p>This evening I’ve been starting work on converting unit tests from [Mocha](<a href="https://mochajs.org/">https://mochajs.org/</a>) to [Jest](<a href="https://facebook.github.io/jest/">https://facebook.github.io/jest/</a>). The reason for doing this is that Jest runs test in parallel, handles ES6 code better, gives a better output and I just want to learn it.</p><p>When I had my tests in Mocha, for stubbing and mocking I used [Sinon](<a href="http://sinonjs.org/">http://sinonjs.org/</a>). This is a perfectly good library, but Jest has this functionality out of the box, so it’s not necessary to have this as an additional dependency. Jest offers a mock in the form of `jest.fn()`. I struggled as I was trying to use this for stubbing out `event.preventDefault()` in an event handler in a React component. The proper way to do this is demonstrated in the below [Gist](<a href="https://gist.github.com/blairg/b6575a23ce96603a120d841f70463f76">https://gist.github.com/blairg/b6575a23ce96603a120d841f70463f76</a>), using [`jest.spyOn(objectToSpyOn, ‘functionToSpyOn()’)`](<a href="https://facebook.github.io/jest/docs/en/jest-object.html#jestspyonobject-methodname">https://facebook.github.io/jest/docs/en/jest-object.html#jestspyonobject-methodname</a>).</p><p>Any questions or corrections required get in touch.</p><p>&lt;script src=”<a href="https://gist.github.com/blairg/b6575a23ce96603a120d841f70463f76.js">https://gist.github.com/blairg/b6575a23ce96603a120d841f70463f76.js</a>&quot;&gt;&lt;/script&gt;</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9fa11f1d355e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Boilerplate for Hapi.js, Web Socket, HTTP/2, React and Redux]]></title>
            <link>https://medium.com/@blairgarrett/boilerplate-for-hapi-js-web-socket-http-2-react-and-redux-e83950c2fa3?source=rss-d9c383a6f02------2</link>
            <guid isPermaLink="false">https://medium.com/p/e83950c2fa3</guid>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[hapijs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Blair Garrett]]></dc:creator>
            <pubDate>Wed, 13 Dec 2017 23:20:49 GMT</pubDate>
            <atom:updated>2017-12-13T23:20:49.616Z</atom:updated>
            <content:encoded><![CDATA[<p>Level — Advanced. Read Time ~ &lt;10–15 minutes.</p><h3>TL;DR</h3><p><a href="https://github.com/blairg/hapi-socket-react-boilerplate">Github Repo</a></p><p><a href="https://hapi-boilerplate-docker.herokuapp.com/index">Running App</a></p><h3>Boilerplate Goals</h3><p>Recently I set myself the goal of learning and gaining a better understanding of the following technologies: -</p><ul><li>NodeJS</li><li>Hapi.js</li><li>React</li><li>Redux</li><li>SSR (server side rendering)</li><li>Webpack</li><li>HTTP/2</li><li>Web Sockets</li><li>Travis</li><li>Heroku</li></ul><p>Some of the above I’ve worked with before and others are new to me. So, I thought the best way of learning the above was to author a simple todo application (could have been “Hello world!”), demonstrating the aforementioned technologies. I took this further in the end and decided to create a boilerplate. My motivations for doing this are to hopefully help others setting up a Hapi application with similar components and tech stack. It’s also a useful reference for myself on how to server side render React and Redux.</p><p>The repo can be found on Github -&gt; <a href="https://github.com/blairg/hapi-socket-react-boilerplate">https://github.com/blairg/hapi-socket-react-boilerplate</a></p><p>The boilerplate is not the most beautifully engineered application out there (happy for criticism of how I can improve it and sharpen my skills). My main intention was to focus on developing shiny features and use it as an educational project. Saying that, I have tried to instill good practices and opinions. I am a firm believer in separating your concerns and having a solid folder structure in place, to ensure your application is both maintainable and extensible.</p><h3>Folder Structure</h3><p>Once you clone the repo and dig into the code, which I advise you do. You will see the below folders in the root: -</p><ul><li>src</li><li>client</li><li>server</li><li>shared</li><li>test</li><li>client</li><li>server</li><li>shared</li></ul><p>My opinions here are to structure your test folders identically to your application folders. Others may disagree, but this synergy has worked for me. Having discipline and attention to detail allows your application to scale. It’s also vital if you intend a small army of developers to support and develop it.</p><p>For this boilerplate, client refers to the React/Redux code and server refers to the Hapi code. Due this being an isomorphic JavaScript application, shared is the bridge between the client and server. All business logic and data access should live here.</p><h3>Client Folder Structure</h3><ul><li>client</li><li>actionTypes</li><li>actions</li><li>components</li><li>containers</li><li>presentation</li><li>reducers</li><li>services</li><li>subscribers</li></ul><p>actionTypes, actions, reducers house the Redux actions and logic. components stores the React components. I&#39;ve decided to break this down even further, by having containers which houses components connected to the Redux store. The purpose of the presentationfolder is to separate your component with there sole purpose to render data passed through their props. subscribers is used for connecting the React components to Web Sockets. Will go into more detail later on.</p><h3>Server Folder Structure</h3><ul><li>server</li><li>controllers</li><li>models</li><li>plugins</li><li>repositories</li><li>routes</li><li>services</li></ul><p>The thought behind the folder structure on the server allows the concerns to be separated. So the controllers folder will contain the concrete implementations. Such as in the index controller which server side renders the React and Redux. I recommend breaking this open to gain a better understanding. plugins is a nice to way to compartmentalise the Hapi plugins away from the main server implementation. routes is an interface to the controller controller implementation. This makes the server very testable as the routes can be mocked.</p><h3>Shared</h3><p>In my implementation I have very little in the shared folder. As this not a real world application and is contrived, there wasn&#39;t much scope for adding code here. But if you intend to create an isomorphic React/Hapi application, any code which is shared by the client and server should reside here. The rewards you reap from this separation will be great and force <a href="https://www.infoq.com/news/2012/05/DRY-code-duplication-coupling">DRY</a> principles.</p><h3>Webpack</h3><p>When I first started developing this application. I had one Webpackfile. This quickly became unwieldy. So I split out into a client and server file to mirror the application separation. The main takeways from this are the following: -</p><ul><li>Client — Targets web and creates a bundle.js in the public\jsfolder. Entry point is the app.jsx React component.</li><li>Server — Targets node and transpiles the ES6 Hapi implementation to ES5. With Node version 9, I’m actually not sure this is needed. Will have to check it out.</li></ul><h3>Docker</h3><p>Got quite a primitive Dockerfile. This may need expanding for more advanced applications. The main gist of it is the following: -</p><ul><li>Use an official NodeJS base image unless you have very good reason not to.</li><li>You’ll see there are a few environment variables passed in. This allows the application to be <a href="https://12factor.net/">12 factor</a> and can be run anywhere.</li></ul><h3>Travis/Heroku</h3><p>You will see in the Travis yml where I actually push the built Docker image up to the Heroku container registry. My Travis file, serves the following purposes in order: -</p><ol><li>Use NodeJS environment running version 8.9.1.</li><li>Run Docker.</li><li>Install the npm modules from the package.json.</li><li>Run the ESLint task.</li><li>Run the code coverage using Mocha and Istanbul.</li><li>Build the Dockerfile.</li><li>Then for master branch on GitHub, login to the Heroku container regsitry and push up the built image.</li><li>Heroku will then run a container based on this image.</li><li>Push coverage stats to <a href="https://coveralls.io/github/blairg/hapi-socket-react-boilerplate?branch=master">Coveralls</a>.</li></ol><h3>Future Work</h3><p>I’d like to learn/add the following to this repo: -</p><ul><li><a href="https://storybook.js.org/">Storybook</a></li><li><a href="https://www.typescriptlang.org/">TypeScript</a></li><li><a href="https://facebook.github.io/jest/">Jest</a></li><li><a href="https://prettier.io/">Prettier</a></li></ul><h3>Questions</h3><p>Any questions about this, leave them below and I’ll try my best to respond.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e83950c2fa3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>