<?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 Greg Kallai on Medium]]></title>
        <description><![CDATA[Stories by Greg Kallai on Medium]]></description>
        <link>https://medium.com/@gerg.kallai?source=rss-945d93e87cb9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*8-QKLQZYMcZZmRLk0kPQDA@2x.jpeg</url>
            <title>Stories by Greg Kallai on Medium</title>
            <link>https://medium.com/@gerg.kallai?source=rss-945d93e87cb9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 16:25:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gerg.kallai/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 restrict your Routes and Links in React.js now with Hooks]]></title>
            <link>https://medium.com/craft-academy/how-to-restrict-your-routes-and-links-in-react-js-now-with-hooks-12b395c1a2fe?source=rss-945d93e87cb9------2</link>
            <guid isPermaLink="false">https://medium.com/p/12b395c1a2fe</guid>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Greg Kallai]]></dc:creator>
            <pubDate>Wed, 30 Oct 2019 20:46:22 GMT</pubDate>
            <atom:updated>2019-10-30T20:46:22.100Z</atom:updated>
            <content:encoded><![CDATA[<p>An example how to create custom routes and links from the react-router-dom package and extract your conditionals</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jeBH-kfAlTYD5zVP" /><figcaption>Photo by <a href="https://unsplash.com/@foxxmd?utm_source=medium&amp;utm_medium=referral">Matt Duncan</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In this article I am going to show you 2 examples how to restrict your <strong>Route</strong> and <strong>Link</strong> components coming from react-router-dom. I am not going into detail how this package works. If you are not familiar with the concept please <a href="https://reacttraining.com/react-router/web/guides/quick-start">check out the docs</a> before continuing with this article.</p><h3>Restricting Routes</h3><p>I have been working with react-router-dom for a while now which is a great package for navigating around in your React application. As you introduce authentication sooner or later you want to prevent unauthorized users from navigating to certain places. Common solution to achieve that is conditionally render your Routes using ternary operators. An example could look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7716cdfada19a3cce6a04fa04868d809/href">https://medium.com/media/7716cdfada19a3cce6a04fa04868d809/href</a></iframe><p>Here we have a basic setup where we store our user information in <a href="https://react-redux.js.org/">redux</a>. We have some routes that are restricted to logged in users, some routes that are restricted for specific user roles (teacher and student role in this case). It is an OK solution, it prevents rendering components that we do not want certain users to see, but it is not complete.</p><p>If the user navigates to &quot;/only-teacher&quot; they will see an empty page. Ideally if a URL is reached which is hid behind some sort of restriction, the user should be redirected, for example to the login or index page. We can achieve that by using <strong>&lt;Redirect /&gt;</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa5f3d87c71aa37121376688632cb8e6/href">https://medium.com/media/fa5f3d87c71aa37121376688632cb8e6/href</a></iframe><p>Now we are truly preventing users from reaching restricted URLs, however our <strong>Content.js</strong> starts to get cluttered and we only have a couple of Routes there. We can simplify this setup by creating our own <strong>CustomRoute.js </strong>component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b50744ab91dd18039a042ee0490bcd7/href">https://medium.com/media/3b50744ab91dd18039a042ee0490bcd7/href</a></iframe><p>To break what is happening here:</p><ul><li>We use 2 hooks that have been added to the <a href="https://reactjs.org/docs/hooks-intro.html">React 16.8</a>: <a href="https://reactjs.org/docs/hooks-state.html">useState()</a> and <a href="https://reactjs.org/docs/hooks-effect.html">useEffect()</a>.</li><li>First we create our state with useState() that will store our component that we want to return. Initially it is empty.</li><li>Then we add the useEffect() hook which acts as <a href="https://reactjs.org/docs/react-component.html#componentdidupdate">componentDidUpdate()</a>. By adding the [props.user] as a secondary argument we will only allow this function to execute when that user prop is changing (which should be during login or logout).</li><li>To get the user we use redux as in the previous example.</li><li>Inside the useEffect() we add a switch statement where we look for the props.condition, which we will add to determine what condition we want to check against when we callCustomRoute. I added 3 examples: isSignedIn, teacher role and student role.</li><li>Inside the case we have a ternary operator. If the condition is satisfied we set the state to the <strong>&lt;Route {…props} /&gt;</strong> which will spread and passes down all of our props down thus rendering our Route and the component we specified.</li><li>By setting the state with setReturnedRoute() the component will be re-rendered and returnedRoute will be equal to <strong>&lt;Route {…props} /&gt;</strong>.</li><li>If the condition is not satisfied the returnedRoute will be set to <strong>&lt;Redirect to=’/index’ /&gt;</strong> and the user will be redirected to that URL.</li><li>Default case is that we do not have any condition, then we just return <strong>&lt;Route {…props} /&gt;</strong></li></ul><p>After importing this <strong>CustomRoute.js</strong> in our previous example we can refactor <strong>Content.js</strong> to achieve the same result like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/054bd5b6b9ba3f83981272ba45938221/href">https://medium.com/media/054bd5b6b9ba3f83981272ba45938221/href</a></iframe><p>It is cleaner, we could remove redux and we could keep adding more conditionals in our <strong>CustomRoute.js </strong>and keep the logic separated there.</p><h3>Restricting Links</h3><p>In many cases we want to hide navigational links from unauthorized users. We could use ternary operators to achieve that similar to the previous Route example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f58071b079776a543cb6e4755282a433/href">https://medium.com/media/f58071b079776a543cb6e4755282a433/href</a></iframe><p>The structure is the same here. We store our user in redux and use ternary operators to show couple of links only for specific roles, and to hide links based whether the user is logged in or not.</p><p>The same way as with routes we could create our own <strong>CustomLink.js</strong> component to extract away this logic:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4464119173f7a0c633657dd9bb44db0/href">https://medium.com/media/e4464119173f7a0c633657dd9bb44db0/href</a></iframe><p>Logic is the same as before:</p><ul><li>We add useState() to store our Link component in.</li><li>Add useEffect() hook to listen to [props.user] changes (login/logout).</li><li>Add a switch case to check against the condition we pass down.</li><li>If we satisfy the condition we return the Link with props and children, if not we return null.</li><li>By default, if there is no condition given we return the Link with props and children.</li></ul><p>After adding that component we can refactor our <strong>Header.js</strong> like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/19c3c64d51d5eb707c6868f6a4e9007a/href">https://medium.com/media/19c3c64d51d5eb707c6868f6a4e9007a/href</a></iframe><p>Again we extracted away the logic, redux, and made the code cleaner.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=12b395c1a2fe" width="1" height="1" alt=""><hr><p><a href="https://medium.com/craft-academy/how-to-restrict-your-routes-and-links-in-react-js-now-with-hooks-12b395c1a2fe">How to restrict your Routes and Links in React.js now with Hooks</a> was originally published in <a href="https://medium.com/craft-academy">Nomads Of Code</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create a dynamic form in React using Hooks]]></title>
            <link>https://medium.com/craft-academy/create-a-dynamic-form-in-react-using-hooks-f64cfabaeaaa?source=rss-945d93e87cb9------2</link>
            <guid isPermaLink="false">https://medium.com/p/f64cfabaeaaa</guid>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Greg Kallai]]></dc:creator>
            <pubDate>Tue, 17 Sep 2019 17:29:12 GMT</pubDate>
            <atom:updated>2019-09-17T17:29:12.297Z</atom:updated>
            <content:encoded><![CDATA[<p>An example of fully dynamic form rendering with Hooks and some other tricks</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Hxth4p57wdBUUDk2" /><figcaption>Photo by <a href="https://unsplash.com/@brookanderson?utm_source=medium&amp;utm_medium=referral">Brook Anderson</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><a href="https://reactjs.org/docs/hooks-intro.html"><strong>Hooks</strong></a><strong> </strong>were added to React 16.8 and increased the number of features that can be used in functional components. The 2 main additions are <a href="https://reactjs.org/docs/react-component.html#the-component-lifecycle"><strong>lifecycle methods</strong></a> and <a href="https://reactjs.org/docs/faq-state.html"><strong>state management</strong></a>.</p><p>The two hooks to achieve that are <a href="https://reactjs.org/docs/hooks-effect.html"><strong>useEffect()</strong></a><strong> </strong>and <a href="https://reactjs.org/docs/hooks-state.html"><strong>useState()</strong></a>. From these two I will use the latter to showcase state management in a dynamic form.</p><p>Let’s start by creating the first building block of our dynamic form, the <strong>Input.js</strong> component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a3a502dccf0f2635543983667617846/href">https://medium.com/media/9a3a502dccf0f2635543983667617846/href</a></iframe><p>We are using couple of things here:</p><ul><li><a href="https://create-react-app.dev/docs/adding-a-css-modules-stylesheet">Css modules</a> for styling. Thanks to the new react version no webpack configuration is necessary, React will recognize css modules if they are created with the <em>module.css</em> tag</li><li><a href="https://www.freecodecamp.org/news/the-basics-of-destructuring-props-in-react-a196696f5477/">Props destructuring</a> for visibility and control over what props are we sending to our <strong>Input.js</strong>.</li><li>Setting default props, for instance placeholder = text<em>. </em>That means if we are not passing down a placeholder prop the text will be used as a default. Another benefit of props destructuring.</li></ul><p>Secondly we should create our custom <strong>Button.js</strong> component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4bd0c1ae98a843768a56579a356ee32c/href">https://medium.com/media/4bd0c1ae98a843768a56579a356ee32c/href</a></iframe><p>Another trick we are using here:</p><ul><li>We are passing the buttonType down, so that we can create custom styles for different type of buttons we want to have in the application, while classes.button provides the basic style. As <strong>Input.js</strong>, this component can also be further customized.</li></ul><p>Now we have our <strong>Input.js </strong>and <strong>Button.js </strong>components, but before putting them to use we should create a file that will hold our input fields and form structures. Create the following file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1bcf4ab8bce361952ec5bf6a416f148c/href">https://medium.com/media/1bcf4ab8bce361952ec5bf6a416f148c/href</a></iframe><p>The fields object holds all possible field that we can add to our forms. This can be easily updated, new fields can be added or the current ones can be customized. I also added a registerForm and loginForm examples. We just need to export these arrays so that we can create the dynamic forms!</p><p>Next we will create the <strong>Form.js</strong> file where we will first use the useState() hook to manage the information in our form, and will also call all of those components we created to action!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bfaca70ac10caf3133c7fc5a07254608/href">https://medium.com/media/bfaca70ac10caf3133c7fc5a07254608/href</a></iframe><p>To break down what is happening here:</p><ul><li>The formStructure prop is the array we export from <strong>formElements.js </strong>file, in this example either registerForm or loginForm. Since these are arrays we can use the map function to iterate through them and render the <strong>Input.js</strong> component. These objects hold all the information necessary for our <strong>Input.js</strong> component, so we just need to pass them down.</li><li>formTitle and buttonText are additional information either used in the <strong>Form.js</strong> itself or passed down to our custom <strong>Button.js</strong> component.</li><li>The last bit is the handleChange() function where we use useState() hook! Both formData and setFormData comes from the hook, however the state is not used in this form, but in the container that renders the form.</li><li>In the function we copy the formData using the spread operator. <strong>This is very important</strong> because the useState() hook — <em>as opposed to </em><em>this.setState() function</em> —<strong> does not automatically merge the state, but replaces it!</strong> Therefore we always have to preserve the previous state.</li><li>After copying we can set the object either create a new key or update the existing one by using this syntax: formDataCopy[event.target.name] = event.target.value</li></ul><p>That is the basic skeleton for our dynamic form! Now we need a container to render it, where we have the actual logic: managing the state and performing the necessary side effects. I am going to show you an example for rendering registration and login form in a common container I call <strong>AuthenticationForm.js</strong> :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c580317a58f3867caed3f60fb15217de/href">https://medium.com/media/c580317a58f3867caed3f60fb15217de/href</a></iframe><p>You could create two separate components for registration and login forms, but there will be very little difference between them. In the name of re-usability and DRY code we will it in one container. So what is happening here?</p><ul><li>const [formData, setFormData] = useState({}) is how we create our initial formData state, which is empty in the beginning, and setFormData() will be the function that will update that state.</li><li>The action function takes that formData and performs the selected function, which is either registerUser() or signInUser(). This is an authentication solution coming from <a href="https://github.com/kylecorbelli/redux-token-auth">redux-token-auth</a> because we are using <a href="https://api.rubyonrails.org/">Rails as the API</a> with <a href="https://github.com/lynndylanhurley/devise_token_auth">devise-token-auth</a>. During the function execution we use a switch statement where we can dynamically set that function based on the props we receive.</li><li>The last function is the onSubmit(), where we perform our action() function, making a HTTP POST request to our API. Upon success reroute to root using the <a href="https://reacttraining.com/react-router/web/guides/quick-start">react-router-dom package</a> or catch errors (more error handling functions can be performed there).</li><li>As a side-note: the withRouter hoc is necessary because we are using the render() method in the &lt;Route /&gt; component (<em>will be shown later</em>), instead of the component prop. Because of that the history object is not available by default, we need to wrap the component with withRouter if we want to use it.</li></ul><p>Finally we need to render the <strong>AuthenticationForm.js </strong>somewhere. I use the react-router-dom package to render the forms on different URLs in <strong>App.js</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/870f9bb4c1ab04a43b399f95e796e09b/href">https://medium.com/media/870f9bb4c1ab04a43b399f95e796e09b/href</a></iframe><p>The loop is completed:</p><ul><li>We import our registerForm and loginForm arrays from our <strong>formElements.js</strong> file which will create our dynamic forms.</li><li>We set the action prop which will determine which function will be used, registerUser() or signInUser().</li><li>We add the formTitle and buttonText to the forms.</li></ul><p>The basic skeleton is ready for further use, after the little lengthy setup it will be much quicker and DRYer to create new forms in our application and without the need for Class components!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f64cfabaeaaa" width="1" height="1" alt=""><hr><p><a href="https://medium.com/craft-academy/create-a-dynamic-form-in-react-using-hooks-f64cfabaeaaa">Create a dynamic form in React using Hooks</a> was originally published in <a href="https://medium.com/craft-academy">Nomads Of Code</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Test elegantly against different API responses in React using Cypress.io]]></title>
            <link>https://medium.com/craft-academy/test-elegantly-against-different-api-responses-in-react-using-cypress-io-8108e535cb69?source=rss-945d93e87cb9------2</link>
            <guid isPermaLink="false">https://medium.com/p/8108e535cb69</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[cypress]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Greg Kallai]]></dc:creator>
            <pubDate>Fri, 06 Sep 2019 21:00:49 GMT</pubDate>
            <atom:updated>2019-09-06T21:00:49.408Z</atom:updated>
            <content:encoded><![CDATA[<p>Create dynamic fixtures to mock API responses using JavaScript Objects or Functions</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TMGcd3m72_fcpKog" /><figcaption>Photo by <a href="https://unsplash.com/@agkdesign?utm_source=medium&amp;utm_medium=referral">Alex Knight</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>1. Introduction</h3><p>As an application grows even small changes can cause unforeseeable consequences. As a developer it is hard to remember every conditional and every effect that a change can have. That is why automated testing is a great way to ensure that your application is working properly.</p><p><a href="https://www.cypress.io/"><strong>Cypress</strong></a><strong> </strong>is a fantastic testing tool for javascript based web applications, and in this article I am going to share a learning, that might help you increase your efficiency.</p><p>If you are working against an API you could not make every call when you are testing. Sometimes you have to use <a href="https://docs.cypress.io/guides/guides/network-requests.html#Testing-Strategies"><strong>mocks</strong></a><strong>,</strong> which allows you to mimic responses coming from an API and test certain scenarios without actually making a call. Cypress has built in solution to do that: you can store these fixtures in the fixtures folder, or add responses directly to <a href="https://docs.cypress.io/api/commands/route.html#Syntax"><strong><em>cy.route()</em></strong></a><strong><em> </em></strong>function, usually as JSON.</p><p>Keeping your fixtures updated can be painful at times, and sometimes you have to check something dynamically.</p><h3>2. Using JavaScript objects as dynamic mocks</h3><p>An example could be that you are restricting something based on date. In that case you cannot use a static mock, you have to create something dynamically. Luckily cypress lets you do that, using <strong>javascript objects as responses</strong>. An example could look like that:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc2cc9e7d6a73a54989eb51f5fc0e4ed/href">https://medium.com/media/bc2cc9e7d6a73a54989eb51f5fc0e4ed/href</a></iframe><p>The sessionpage variable we declare at <em>line 5</em> ending it at <em>line 85 </em>is a javascript object which we will add to the<em> cy.route()</em> function at <em>line 90</em> as a response. In there we are creating 2 sessions, the first with a dynamic start/end <em>(line 12 - line 13) </em>date using today <em>(line 4)</em> as a reference to test against some conditionals in our codebase, and the second with a fixed date where it does not matter. This works but clutters our test greatly.</p><p>Since we are in JavaScript we could easily extract this object and import it in our test, as we would do it with a helper method in our React components:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/357d135d1aec624c9b1b9cc05e9da851/href">https://medium.com/media/357d135d1aec624c9b1b9cc05e9da851/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/90b169c022e2125af5a5705638e9a437/href">https://medium.com/media/90b169c022e2125af5a5705638e9a437/href</a></iframe><p>Instead of using the local variable sessionpage we import it from the new file we created <em>(at line 3)</em> where export the same object as default, and use it in our <em>cy.route()</em> function <em>(at line 10)</em>.</p><p>This clears up our tests <em>(37 lines instead of 117)</em>, makes it easily readable, and opens up even more possibilities in our mock object.</p><h3>3. Using JavaScript functions as dynamic mocks</h3><p>I had cases when I had to create 2 different mock files just because 1 or 2 attributes were different. To avoid this you can create javascript functions as a dynamic mock. You can replace static attributes with dynamic references, add default state, and pass down values you want to change. The structure I choose is similar to redux reducers/actions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36b2d46a6368d1b8e54af9e5e49a3b47/href">https://medium.com/media/36b2d46a6368d1b8e54af9e5e49a3b47/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6294dc6fed841b2a456ce6c073fe36d/href">https://medium.com/media/b6294dc6fed841b2a456ce6c073fe36d/href</a></iframe><p>In this last example I made the mock even more responsive, so if you need different statuses in different it blocks you can pass down different payloads in the response if you convert your dynamic mock into a function.</p><p>In the <em>sessionpage.js</em> file I add defaults object as default and use it as a reference in the the <em>sessionpage()</em> function on <em>line 22</em> and <em>line 61</em>. If I do not pass down anything the default object will be used. However if I pass down an argument in the function that will replace the defaults I added. That is what I do in <em>line 10</em> in <em>users_can_access_videocall_spec.js.</em></p><p>Since you have a function now as a mock you can do so much more. You can add the number of objects you want to return in the payload, you perform calculations, and add random effects. The possibilities are endless!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8108e535cb69" width="1" height="1" alt=""><hr><p><a href="https://medium.com/craft-academy/test-elegantly-against-different-api-responses-in-react-using-cypress-io-8108e535cb69">Test elegantly against different API responses in React using Cypress.io</a> was originally published in <a href="https://medium.com/craft-academy">Nomads Of Code</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The 2 minute guide for creating code snippets in VS Code]]></title>
            <link>https://medium.com/craft-academy/the-2-minute-guide-for-creating-code-snippets-in-vs-code-7ff2ca9194c3?source=rss-945d93e87cb9------2</link>
            <guid isPermaLink="false">https://medium.com/p/7ff2ca9194c3</guid>
            <category><![CDATA[vscode]]></category>
            <category><![CDATA[hacks]]></category>
            <category><![CDATA[snippet]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Greg Kallai]]></dc:creator>
            <pubDate>Thu, 23 May 2019 14:14:30 GMT</pubDate>
            <atom:updated>2019-05-23T14:14:30.132Z</atom:updated>
            <content:encoded><![CDATA[<p>Boost your coding productivity</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TZqY42sUG6k7kpRI" /><figcaption>Photo by <a href="https://unsplash.com/@isaacmsmith?utm_source=medium&amp;utm_medium=referral">Isaac Smith</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Code snippets are an amazing tools to enhance your coding speed. There are extensions you can download for the different languages and frameworks that provide you with great variety of them, but sometimes you can’t find what you are looking for. Not to worry many code editor let’s you create your own code snippets.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ajdm25OSthtvXZdVj4zOMA.gif" /><figcaption>An example of creating a React component with 3 clicks</figcaption></figure><p>In this example we are going to take a look at the code editor I use: Visual Studio Code.</p><p>To create a snippet do the following steps:</p><ul><li>Press in <strong>Ctrl+Shift+p</strong> (<strong>Command+Shift+p</strong> on macOS) in your VS Code</li><li>Start typing in <strong>snippet</strong></li><li>Select <strong>Preferences: Configure User Snippets</strong></li><li>Click on <strong>Create a new global snippets file …</strong> (here you can select previous files if you already have one, or you can create local project specific snippets)</li><li>Add a name for the file and save:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fUJKlkzQK8e7KHeQU903iw.gif" /></figure><p>VS Code snippets follow the same structure:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9351ea1e6d413ee150e5af497e3ef0c/href">https://medium.com/media/f9351ea1e6d413ee150e5af497e3ef0c/href</a></iframe><ul><li>The <strong>prefix</strong> is what you<strong> type in the editor</strong> to call the snippet.</li><li><strong>Body</strong> is an array of strings. <strong>Every element represents a single line from your code</strong>. You can use the syntax: <em>${1:Example}</em> which will be highlighted for you when you call the snippet.</li><li><strong>Description</strong> is what is going to be visible in the editor when you start typing out your snippet.</li></ul><p>Save the file and the end result should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/933/1*8ODoQvrdvPx-K1t4w_xdsA.gif" /><figcaption>A standard arrow function snippet</figcaption></figure><p>There is a site called <a href="https://snippet-generator.app/"><strong>snippet-generator</strong></a><strong> </strong>(kudos to <a href="https://pawelgrzybek.com/">Pawel Gryzbek</a>) which quickly transform your everyday code to the above mentioned format, which only leaves you to create the prefix and the description.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8MLGWw3JzzzNuNQcfCN06A.png" /><figcaption>source: <a href="https://snippet-generator.app/">snippet-generator</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ff2ca9194c3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/craft-academy/the-2-minute-guide-for-creating-code-snippets-in-vs-code-7ff2ca9194c3">The 2 minute guide for creating code snippets in VS Code</a> was originally published in <a href="https://medium.com/craft-academy">Nomads Of Code</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to leverage LocalStorage in Cypress testing]]></title>
            <link>https://medium.com/craft-academy/how-to-leverage-localstorage-in-cypress-testing-3fdc04628868?source=rss-945d93e87cb9------2</link>
            <guid isPermaLink="false">https://medium.com/p/3fdc04628868</guid>
            <category><![CDATA[cypress]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[mocking]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Greg Kallai]]></dc:creator>
            <pubDate>Mon, 06 May 2019 13:37:00 GMT</pubDate>
            <atom:updated>2019-05-06T14:04:44.289Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to hack log-in in React Automated testing</h3><blockquote>With a sample Cypress.io test. Co-authored by: <a href="https://medium.com/u/98456604b7f3">Aditya Naik</a></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*bYfMcXAIZVYwyUiz" /><figcaption>Photo by <a href="https://unsplash.com/@dtravisphd?utm_source=medium&amp;utm_medium=referral">David Travis</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><a href="https://www.cypress.io/"><strong>Cypress</strong></a> is a great end to end testing framework for browser based applications. It allows you to monitor your test specs in real time, and offer great variety of tools for debugging.</p><p>As the application matures, the test suit gets bigger and bigger. To keep the test run time under control, we need to sanitize and DRY the tests.</p><p>One prime candidate to optimize at the onset is the login flow. Most functionalities we will test will involve user logging in at the start, and opening page every time, loading the login form/dialogue and filling in user credentials every time becomes highly time consuming.</p><p>We are going to take a look how to simplify that in a <strong>React app </strong>using <strong>Rails as back-end API</strong>.</p><p>In this example we are using <a href="https://github.com/lynndylanhurley/devise_token_auth"><strong>devise-token-auth</strong></a><strong> </strong>in the Rails API, and <a href="https://github.com/kylecorbelli/redux-token-auth"><strong>redux-token-auth</strong></a> in the React app. We are going to stub out our response, but as a rule of thumb this <strong>should be avoided</strong>:</p><blockquote><em>When requests are not stubbed, this guarantees that the contract between your client and server is working correctly. — </em><a href="https://docs.cypress.io/guides/guides/network-requests.html#Testing-Strategies">Cypress official documentation</a></blockquote><p><strong>Redux-token-auth </strong>is a token based authentication token which simply means that our API will identify us via a token. It will always look for your credentials in your LocalStorage (via the inbuilt <em>verifyCredentials()</em> function). The first time you log in, a unique token is created and stored.</p><p>Once it has this stored token available, it sends it across to the API to confirm. API responds with a successful authentication response and <strong>redux-token-auth</strong> logs you in without you filling in the login form again.</p><p>We can mimic this in our tests to improve our testing performance!</p><p>To start with, we need all the authentication related information stored in our LocalStorage. We can inspect this in our chrome browser using the chrome developer tools:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/815/1*t5P-ScU1DQDSJz7sP2s-Nw.png" /></figure><p>Now that we have the information we can add them into our LocalStorage in our test environment:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99d789a2c7bbce76277216c6d866e9d1/href">https://medium.com/media/99d789a2c7bbce76277216c6d866e9d1/href</a></iframe><p>Now when we run the test <strong>redux-token-auth</strong> will try to authenticate your user, but it will fail, because the token expired. To prevent this we can stub out the response and make our user authenticated all the time. You should be aware of your payload when being authenticated but if you are unsure you can use the network tab in the chrome developer tools to check it.</p><p>Cypress ships with a couple of support files, one of them is cypress/support/commands.js. Here we can add custom functions with the Cypress.Commands.add function. We will leverage this to keep our integration files DRY. In our example the uid is the email address of the user. We can use that to make this method dynamic:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a274d92b259706c0ff4b4c456083e28/href">https://medium.com/media/4a274d92b259706c0ff4b4c456083e28/href</a></iframe><p>Also by adding the cy.visit at the end of this custom function we trigger the automatic log in function of <strong>redux-token-auth. </strong>Now we can use this method in our test files to authenticate the users quickly without needing to use the user interface.</p><p>Here is an example how this method looks in action:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c8067729fbccdc8f106a2b9a6b53f33d/href">https://medium.com/media/c8067729fbccdc8f106a2b9a6b53f33d/href</a></iframe><p>As you can see, we log in user using our custom defined method, before accessing user specific information.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3fdc04628868" width="1" height="1" alt=""><hr><p><a href="https://medium.com/craft-academy/how-to-leverage-localstorage-in-cypress-testing-3fdc04628868">How to leverage LocalStorage in Cypress testing</a> was originally published in <a href="https://medium.com/craft-academy">Nomads Of Code</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>