<?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 Ruben Oostinga on Medium]]></title>
        <description><![CDATA[Stories by Ruben Oostinga on Medium]]></description>
        <link>https://medium.com/@RubenOostinga?source=rss-142a01aea154------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*TM_pDAcHBEKnAJ7wMlxZTQ.jpeg</url>
            <title>Stories by Ruben Oostinga on Medium</title>
            <link>https://medium.com/@RubenOostinga?source=rss-142a01aea154------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 19:47:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@RubenOostinga/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[Avoiding deeply nested component trees]]></title>
            <link>https://medium.com/@RubenOostinga/avoiding-deeply-nested-component-trees-973edb632991?source=rss-142a01aea154------2</link>
            <guid isPermaLink="false">https://medium.com/p/973edb632991</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[web-components]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[angularjs]]></category>
            <dc:creator><![CDATA[Ruben Oostinga]]></dc:creator>
            <pubDate>Thu, 22 Jun 2017 08:27:17 GMT</pubDate>
            <atom:updated>2017-06-22T08:27:17.265Z</atom:updated>
            <content:encoded><![CDATA[<p>By passing child components down instead of data you can avoid passing data down through many levels of components. It also makes your components more reusable. Even multibrand components become much easier to build. Overall it is a pattern which improves your frontend code a lot!</p><h3>The Problem</h3><p>When building frontends you will pass data from a parent component to a child component. Often the child component renders this data, but not the component passing it along. The child components have different data requirements than your current component.</p><p>Then you add a new component, somewhere down your component tree. It has new data requirements, so you have to pass its data through all its parent components. On top of data, it might also need callbacks to provide interactivity. You also pass these through all parent components. You change a lot of files to add new functionality. With all the data passing the readability of your code also decreases. Overall the maintainability of codebase decreases.</p><p>The code examples in this post are using React. But you can encounter the same issues in Angular, AngularJS, Vue, Polymer or any other component based frontend library.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7bf4cc56f786e4aba66b9239cc41c881/href">https://medium.com/media/7bf4cc56f786e4aba66b9239cc41c881/href</a></iframe><p>The TabBar components is passing the tabs to its child component. However, it does not need the data for its own view rendering. In this example this is not a big issue. However, in real world applications you will have much more nesting. Then you end up just passing the data along through all these levels of nesting. This adds noise throughout your codebase.</p><p>In essence your component has multiple responsibilities. Next to its regular task of showing UI it is also passing data to its children. When we avoid this we can follow the single responsibility principle! A component is either passing data to child components OR it is rendering data to the screen.</p><h3>The Solution</h3><p>Instead of passing data to child components, you can pass child components to your current component. Parent components can provide these child components with their data . That way you move the responsibility of passing data to your parent component. Your component itself has become a container component. It is now only responsible for displaying its own data in the view.</p><blockquote>Pass components down, not data</blockquote><p>In the code below the TabBar component receives Tab components via the children property. As a side effect of making TabBar a container, it is now also a lot more reusable. We can pass in any <a href="https://facebook.github.io/react/docs/jsx-in-depth.html#children-in-jsx">child component</a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e320103581db1918b00c397fdf9200f7/href">https://medium.com/media/e320103581db1918b00c397fdf9200f7/href</a></iframe><p>This approach is a form of <a href="https://en.wikipedia.org/wiki/Inversion_of_control">inversion of control</a>. You might even consider it a form of dependency injection. The difference is however, that you are not giving up control to a framework, but instead to a different part of your own code.</p><p>This pattern can be applied in other frontend libraries as well. In Angular it is called <a href="https://toddmotto.com/transclusion-in-angular-2-with-ng-content">transclusion</a>. In Polymer / Webcomponents <a href="https://developers.google.com/web/fundamentals/getting-started/primers/shadowdom#composition_slot">slots</a> can be used inside shadow dom.</p><p>Taking the pattern further you will end up with a page component which connects state to all the components. This makes it the only type of component that is passing data down to child components. To make sure it still has a single responsibility we want to avoid also rendering UI in this component.</p><p>Depending on your application your page component might become rather big. Note, this only happens when are truly showing your user a lot of different components. If this is becoming a real issue you can of course trade in some flexibility and add in a level of nesting.</p><h3>Passing multiple child components</h3><p>Some components are not simple wrappers around its child components. They might contain child components at multiple locations. You can consider these places slots. Think of a header component which has a logo as well as a search bar. Both are child components of the header. To apply our component passing pattern to those components as well, we can do the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/53015f5fd3b8dece5d92256b2d61d12a/href">https://medium.com/media/53015f5fd3b8dece5d92256b2d61d12a/href</a></iframe><h3>Multi brand components</h3><p>A special form of reusability is using the same components across multiple brands. When you have a deeply nested component tree and you need to change a single leaf component you have 2 options. Either recreate the entire tree or make the leaf component configurable with an if/else construct.</p><p>When your tree is shallowly nested using container components, you just have to duplicate the page component and swap out the leaf. This type of duplication is not a bad thing. You can see it as the configuration of your page. Often you will be swapping out many components to create a new brand page. Therefore the actual duplication will be limited.</p><h3>Conclusion</h3><p>When you start off building your application it is doesn’t have much nesting and the code is easy to read. This changes when the application starts to grow. You recognize you might have an issue when you are editing many parent components just to get the data / callback you need. By passing components down instead of data you can remove the responsibility of passing data from your components. By doing less in your components, your code becomes easier to maintain and reuse.</p><p>In my experience we can apply this pattern to almost all components. The main use case where you see most value, is if you are building components which are reused for multiple brand variations of your app. If you are not doing this you can use the pattern to avoid your component tree to be too deeply nested.</p><p>I am interested to hear what you think and whether the pattern in this blogpost is something you have used in your projects. Contact me on Twitter or in the comments section.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=973edb632991" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Design by contract using GraphQL]]></title>
            <link>https://medium.com/xebia/design-by-contract-using-graphql-c53ab637ca5?source=rss-142a01aea154------2</link>
            <guid isPermaLink="false">https://medium.com/p/c53ab637ca5</guid>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Ruben Oostinga]]></dc:creator>
            <pubDate>Mon, 20 Feb 2017 16:05:40 GMT</pubDate>
            <atom:updated>2017-02-20T20:45:24.136Z</atom:updated>
            <content:encoded><![CDATA[<p>When interfacing between systems it is good practice to think about the interface design prior to developing the systems. GraphQL can be a useful tool to write down these design decisions using its schema definition language. Even when you are not using GraphQL itself in production. GraphQL’s schema can be used to generate a mock server for clients and can verify whether the responses of the server are valid. This way a clear and precise agreement on the API can be made upfront to avoid costly surprises at the end of the development phase.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*1JVresKVf_zvTAu7Yilr2Q.png" /></figure><p>Although everyone is a fullstack developer nowadays, it is still often the case that the frontend and the backend are not developed by the same person or even the same team. Even when a developer could implement a functionality end to end it could be beneficial to have two developers work in parallel on the backed and the frontend. This means that when you are working on a new feature which depends on new data from the backend there needs to be some communication between the frontend and backend developer. This will ensure upfront that the two systems will integrate properly.</p><p>Far too often I’ve seen the last-minute refactoring because the data provided by the backend does not match the data that was assumed to be provided by the frontend developer. Having a contract ensures everyone is on the same page from the beginning. It also forces everyone to think about all the fields upfront. Sometimes it a single field that is unavailable can cause the impact of a change to be much larger initially anticipated.</p><p>Writing down the interface definitions prior to development is called designing by contract. GraphQL’s schema is the contract that can verify JSON responses of the server. It is fully statically typed and can even make the distinction between optional and required fields.</p><h3>Making the schema</h3><p>GraphQL schemas can be created using plain JSON as well as using its <a href="http://graphql.org/learn/schema/">schema language</a>. Typically this schema is used to validate GraphQL queries but it can also validate any JSON structure.</p><p>As an example we will create a product search API like you might have at an online retailer. A schema should have query as a root of its schema. In this Query type we define a search endpoint. It uses the offset and limit parameters to allow pagination. By default the offset is 0. It returns a SearchResult which has a mandatory array of results of non-null Product&#39;s.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4bceadeaf0af962edbb9b80b25bc700d/href">https://medium.com/media/4bceadeaf0af962edbb9b80b25bc700d/href</a></iframe><p>GraphQL’s schema language is more powerful than shown in this example, but that is outside of the scope of this blogpost.</p><h3>Using the schema to generate mocks</h3><p>To use the schema it needs to be compiled. We’re gonna use graphql-tools from the Apollo team to do this. graphql-tools provides a clean API to parse the schema as well as implement it. It also has functionality to automatically generate a mock implementation. It is only an API abstraction which uses Facebook&#39;s <a href="https://github.com/graphql/graphql-js">graphql-js</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97ba252d98352a95e8492b0d1fdf9aa3/href">https://medium.com/media/97ba252d98352a95e8492b0d1fdf9aa3/href</a></iframe><p>In this code we read the schema definition which is located at schema.graphql from the filesystem. Then we parse it by calling makeExecutableSchema and add a mock implementation. A small gotcha is that makeExecutableSchema requires an array of schema definitions.</p><p>That is all you need to do to create a working GraphQL schema. However, the default mock implementation generated by graphql-tools always returns Hello World as a text value and also generates negative numbers for the total number of products. To fix this we use the casual library to generate mock data. It can easily generate mock data with varying requirements.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/61b30fcb251eb43ea9168a1c61ab9879/href">https://medium.com/media/61b30fcb251eb43ea9168a1c61ab9879/href</a></iframe><p>Note you only need to specify mock data for the fields where you want to override the default mock behavior of graphql-tools. In this example we are fine with the mock generated for the id field and therefore don&#39;t specify mock data for that field.</p><h3>Building a GraphQL server</h3><p>Now that we have a parsed schema we can serve it and use <a href="https://github.com/graphql/graphiql">GraphiQL</a> as a schema browser. We are using Facebook’s <a href="https://github.com/graphql/express-graphql">express-graphql</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/70f8eaacd515a0df4c75d6a2f56da40d/href">https://medium.com/media/70f8eaacd515a0df4c75d6a2f56da40d/href</a></iframe><p>Browsing to http://localhost:3000/graphql allows you to check whether everything works as expecting. You can use GraphiQL&#39;s schema browser as well as its autocomplete to construct queries. Note this is only needed when you are going to use GraphQL in production. It is also possible to just use GraphQL during development.</p><h3>Querying your server</h3><p>Querying a GraphQL server requires a query being sent to a server. An example query which fetches all of the product data can look like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dcabe807898b792b4974c94b1eab174f/href">https://medium.com/media/dcabe807898b792b4974c94b1eab174f/href</a></iframe><p>To send this to the server you could use cURL or use the fetch API like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf6cff418b35d93cf70fd4599ab0bcc3/href">https://medium.com/media/bf6cff418b35d93cf70fd4599ab0bcc3/href</a></iframe><p>This prints a mock result like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a874068d104eed37da31d2f8550093bd/href">https://medium.com/media/a874068d104eed37da31d2f8550093bd/href</a></iframe><h3>Validating JSON values</h3><p>If you were to replace the mock implementation of the schema with actual calls to a backend you get automatic response validation. You can also make a validation tool which doesn’t require any JavaScript knowledge from the backend engineer.</p><p>graphql-js offers a method isValidJSValue that works on input types which can be found in the parsed schema we created before. However, we want to validate the SearchResult type which is a response type. Fortunately <a href="https://github.com/nodkz/graphql-compose">graphql-compose</a> can help to convert a response type to an input type.</p><p>It makes sense that graphql-js can only validate input types by default because those represent the request payload which need to be fully valid. Response payloads only need to have valid fields when they are requested. Fields that are not queried are not validated. Additionally input types can&#39;t have more fields than defined in the schema whereas when response types have fields which are not defined in the schema, they are ignored and not sent to clients.</p><p>Using graphql-compose and isValidJSValue from graphql-js to validate SearchResult values could look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f0897f21c6e16de7935bb4af861704e2/href">https://medium.com/media/f0897f21c6e16de7935bb4af861704e2/href</a></iframe><p>errors will be an array of strings:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/396d6afb288343770b9c63e82a438bc5/href">https://medium.com/media/396d6afb288343770b9c63e82a438bc5/href</a></iframe><p>These error messages can be understood by any developer without much prior knowledge of GraphQL or JavaScript. Using this validation code a developer friendly command line tool can be created so no JavaScript knowledge is required for a backend developer to validate their response payloads. An example of such a commandline tool is available on <a href="https://github.com/0xR/design-by-contract-with-graphql/blob/master/src/validator.js">GitHub</a>.</p><h3>Conclusion</h3><p>GraphQL’s schema language allows the definition of complex data structures. Although this example is using the JavaScript implementation, <a href="https://github.com/graphql-java/graphql-java/pull/243">graphql-java</a> and <a href="http://sangria-graphql.org/learn/#based-on-idl-definitions">sangria-graphql</a> for Scala also support the schema language. However, it seems like the tooling to generate mocking for those languages currently seems to be lacking. Implementing that functionality would be good way to contribute.</p><p>All code for this blogpost is available on <a href="https://github.com/0xR/design-by-contract-with-graphql">GitHub</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c53ab637ca5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/xebia/design-by-contract-using-graphql-c53ab637ca5">Design by contract using GraphQL</a> was originally published in <a href="https://medium.com/xebia">Xebia</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Combining Chai and Jest matchers]]></title>
            <link>https://medium.com/@RubenOostinga/combining-chai-and-jest-matchers-d12d1ffd0303?source=rss-142a01aea154------2</link>
            <guid isPermaLink="false">https://medium.com/p/d12d1ffd0303</guid>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Ruben Oostinga]]></dc:creator>
            <pubDate>Thu, 03 Nov 2016 14:41:50 GMT</pubDate>
            <atom:updated>2016-11-04T21:37:10.795Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<p>In this post I am going to show how to combine both <a href="http://chaijs.com/">Chai</a> and <a href="https://facebook.github.io/jest/">Jest</a> matchers in the same codebase. This can help migrating existing <a href="https://mochajs.org/">Mocha</a>/Chai tests to Jest.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*Q26gw-kNzOXUqZKRr04T-g.png" /><figcaption>Jest logo</figcaption></figure><p>Since <a href="https://facebook.github.io/jest/blog/2016/07/27/jest-14.html">Jest 14 released snapshot testing</a> I have been wanting to use snapshot testing with my existing Mocha codebase which was using Chai matchers. First I was going to use <a href="https://github.com/suchipi/chai-jest-snapshot">a library</a> to do this and I submitted a talk about this to the <a href="http://reactnl.org/">ReactNL conference</a>.</p><p>Having submitted the talk I got contacted by <a href="https://medium.com/u/9ea60eb6fc7c">Christoph Pojer</a> (who develops Jest) and he asked me to consider migrating to Jest instead of using a snapshot testing library. Jest has snapshot testing integrated in the CLI and it could also work with my existing <a href="https://webpack.github.io/">Webpack</a> setup. To give a full picture in my talk I figured I should at least see how difficult it is to migrate. During the process I began to appreciate the nice integration of snapshot testing as well as the powerful mocking features in Jest. I also realized that our current testing setup using <a href="https://github.com/zinserjan/mocha-webpack">mocha-webpack</a> was slow and unpredictable. So I decided to do the migration for real on our production codebase.</p><p>However, long technical migrations are not much fun and don’t deliver any business value. Therefore I don’t want to have to convert all my existing assertions at once. Especially since we’re also using some Chai plugins. These don’t map directly to the standard matchers in Jest. We had around 700 tests to migrate in 120 test suites.</p><p>In the most basic case you could alias the global expect to something like “jestExpect”. However, this was not working due to some internals of Jest. After some discussing I fixed this with <a href="https://github.com/facebook/jest/pull/1731">a PR</a>. This was released in <a href="https://facebook.github.io/jest/blog/2016/10/03/jest-16.html">Jest 16</a>.</p><p>Now it is possible to use Jest and Chai together by aliasing the Jest global “expect”:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a143258ad84800afd3a9c51ff36d7c91/href">https://medium.com/media/a143258ad84800afd3a9c51ff36d7c91/href</a></iframe><p>This code can be put in the <a href="https://facebook.github.io/jest/docs/configuration.html#setuptestframeworkscriptfile-string">setupTestFramework</a> script. Now you can write code like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/105e5b9786aa7fab510a505d30b8e556/href">https://medium.com/media/105e5b9786aa7fab510a505d30b8e556/href</a></iframe><p>This works, is easy to implement and explicitly shows which assertion standard you are using. If it doesn’t bother you that there are two ways to do expectations you can stop reading.</p><p>For some reason it did bother me and after some tinkering I discovered it is also possible to get it to work like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/61361a4e65ef1fc2e2eed7efacd1e1e1/href">https://medium.com/media/61361a4e65ef1fc2e2eed7efacd1e1e1/href</a></iframe><p>We can do both Jest and Chai expectations with the same global “expect”! A big advantage of developing for unit testing is that is easy to do in a test-driven approach. However, when my test failed I still had to get into the NodeJS debugger to investigate what was going on internally.</p><p>Getting this to work is difficult because accessing the “not” property of Chai causes a flag to be set which negates the whole expectation. Also the “not” property is read-only while we need to write to it to merge it with the “not” property of the Jest expectations.</p><p>But as always, a snippet of code says more than a thousand words:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/02773718a298dd1371edad27869bf4f2/href">https://medium.com/media/02773718a298dd1371edad27869bf4f2/href</a></iframe><p>This shows that due to the dynamic nature of JavaScript you can do some powerful monkeypatching. Put that in your <a href="https://facebook.github.io/jest/docs/configuration.html#setuptestframeworkscriptfile-string">setupTestFramework</a> script and it will work as expected.</p><p>Now I can finally write the expectation this all started with:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7368c179be5d30271cbdba909cd37a25/href">https://medium.com/media/7368c179be5d30271cbdba909cd37a25/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d12d1ffd0303" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>