<?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 Tega Oke on Medium]]></title>
        <description><![CDATA[Stories by Tega Oke on Medium]]></description>
        <link>https://medium.com/@codebarz?source=rss-5e3ab1af7c25------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*gJII1mEthA7mOrcmI_iMmQ.jpeg</url>
            <title>Stories by Tega Oke on Medium</title>
            <link>https://medium.com/@codebarz?source=rss-5e3ab1af7c25------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 02:11:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@codebarz/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[Modeling a Polling System with Prototype Oriented Programming in Javascript]]></title>
            <link>https://codebarz.medium.com/modeling-a-polling-system-with-prototype-oriented-programming-in-javascript-c810640fdc62?source=rss-5e3ab1af7c25------2</link>
            <guid isPermaLink="false">https://medium.com/p/c810640fdc62</guid>
            <category><![CDATA[object-oriented-program]]></category>
            <category><![CDATA[oop-concepts]]></category>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[oop]]></category>
            <dc:creator><![CDATA[Tega Oke]]></dc:creator>
            <pubDate>Tue, 04 May 2021 00:40:09 GMT</pubDate>
            <atom:updated>2021-05-04T00:40:09.857Z</atom:updated>
            <content:encoded><![CDATA[<p>In programming generally, we often want to find ways to make our codes reusable so we can write fewer codes or look for a way to avoid duplicating functions or methods across our projects. This is one of the reasons behind Object-Oriented programming</p><p>The key characteristics of every Object-Oriented programming language are that it should support <a href="https://en.wikipedia.org/wiki/Polymorphism_(computer_science)">polymorphism</a>, <a href="https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)">encapsulation</a>, abstraction, and <a href="https://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29">inheritance</a></p><p>In Javascript, inheritance is implemented via its prototype hence the concept behind Prototype-Oriented programming. Objects have a special property, this object is called “a prototype”. This is either null or reference another object.</p><p>To explain this concept, we are going to model a simple polling system</p><h3>Let’s get started</h3><p>A simple polling system should have a person organizing the event and some voters. Taking all that into an app will entail both the person organizing (Admin) and the voter to create an account. The Admin would be able to create an event and add contestants to that event while the voter would be able to vote. These are unique to each User but what’s similar is that both admin and voter would be able to create an account and also view the results of the polls.</p><p>First, we are going to create a mock database to temporarily store our data</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/13c67ac56d12b65affa4cf1879b5b2c2/href">https://medium.com/media/13c67ac56d12b65affa4cf1879b5b2c2/href</a></iframe><p>Then create a <a href="https://www.w3schools.com/js/js_object_constructors.asp">constructor function</a> that would take two parameters for the name and password of the user and also the create account method. We are going to call this function Admin</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a9a38cedbbe79ec179634bff3c9af28/href">https://medium.com/media/5a9a38cedbbe79ec179634bff3c9af28/href</a></iframe><p>Great, we have created our constructor. Now let’s see what happens when we try to create an account.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f8799e99e751e17ac4adc89728b6046f/href">https://medium.com/media/f8799e99e751e17ac4adc89728b6046f/href</a></iframe><p>Now let’s create another constructor for our voters</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd275a61288469b97a201839fab6b630/href">https://medium.com/media/dd275a61288469b97a201839fab6b630/href</a></iframe><p>The same thing that happened in admin happens in voters when we create an account with the .createAccount method. The only issue here is both the user and admin technically do the same thing( submit to the same database but into different arrays and output the same structure of result) and we still repeated the code.</p><h3><strong>Javascript Prototypes</strong></h3><p>What if there was a way to have the .createAccount method in a single constructor for both the Admin and Voter to <strong>inherit</strong> from. This reason here is why the prototype object was implemented in Javascript. For instance, our Admin constructor comes with a prototype object. Let’s try to access Admin prototype in our browser’s console</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mXTCRHdIhsGHvC5fkXJeoQ.png" /><figcaption>Accessing constructor prototype in Javascript</figcaption></figure><p>If you noticed, just like any other Object property in javascript, we accessed our prototype with the dot notation . . By default, a constructor prototype has a constructor property that is set to the constructor itself. In other words</p><p>Admin.prototype.constructor = Admin</p><p><strong>Refactoring the codes</strong></p><p>Let’s refactor our code to implement the prototype concept</p><p>Since both the Admin and Voter are users in our app, we can create a new constructor called User that carries the same properties as the Admin and Voter.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c7a7c1677ef6557d273765b404310a6/href">https://medium.com/media/9c7a7c1677ef6557d273765b404310a6/href</a></iframe><p>Hold on a minute, we are going to add our createAccount method but this time on the User prototype</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fff6348d45c9a03c90130d7cfe2ac288/href">https://medium.com/media/fff6348d45c9a03c90130d7cfe2ac288/href</a></iframe><p>There, our first step to inheritance. We already catered for both Admin and Voter in just one function(we would set the role in the condition soon). Now let’s make Admin and Voter inherit the .createAccount method from User constructor</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/372d4a0e152cb75b9e00f8f3ebc51ea0/href">https://medium.com/media/372d4a0e152cb75b9e00f8f3ebc51ea0/href</a></iframe><p>In the snippet above, we used the <a href="https://developer.mozilla.org/ms/docs/Web/JavaScript/Reference/Global_Objects/Object/create">Object.create</a> method to create a copy of the User prototype in the Admin prototype (note that this copy would have the same reference in memory, so if we refactor the method in the User, it also changes in Admin). Now that Admin has inherited from User lets see what’s in Admin prototype with the browser console</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-E2C84RvXolGM3y9Qz6opw.png" /></figure><p>Because the Admin is inheriting from User, the Admin constructor is set to User. Since we want to inherit only the Admin prototype and not the constructor itself let’s fix this</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d07ddc4477529159812ddde27ea8752/href">https://medium.com/media/8d07ddc4477529159812ddde27ea8752/href</a></iframe><p>Now check using your console again</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X5xXKutuinVQvFvBWnCF2Q.png" /></figure><p>There you go, our Admin constructor is now back to Admin. If you notice, we can see our .createAccount method is now in the prototype of Admin. Now let&#39;s try to create an admin account</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b4165abb7b14d10a4f918ada74ceb1e/href">https://medium.com/media/5b4165abb7b14d10a4f918ada74ceb1e/href</a></iframe><p>Let’s make Voter inherit from user to. Just the same way as admin</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f8eff87f659a700f65a2ee3f9eb14c9/href">https://medium.com/media/1f8eff87f659a700f65a2ee3f9eb14c9/href</a></iframe><p>We created the same .createAccount method in both Admin and User but this time by not duplicating codes and writing fewer codes.</p><p>Now let’s add some more features to our poll app.</p><p><strong>Search Event and View results</strong></p><p>Both the Admin and Voter should be able to search for an event and view the results of the polls, so we would put this method in the User construct prototype so both can inherit it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2cfc333720f678c3e1f0a1e96930cb20/href">https://medium.com/media/2cfc333720f678c3e1f0a1e96930cb20/href</a></iframe><p><strong>Create and Delete Event</strong></p><p>Since Admin would be creating the event then the Admin should be the only one allowed to delete. So we would put this on the Admin prototype and not the User prototype so the Voter won’t inherit this method</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc6a0dea7cfe3c9915bb69b3cb9ee28c/href">https://medium.com/media/fc6a0dea7cfe3c9915bb69b3cb9ee28c/href</a></iframe><p><strong>Vote a Contestant</strong></p><p>And finally, we need to create a method to allow the voter vote for a particular contestant. This would be put on the Voter prototype and not the User so the Admin doesn’t inherit this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d038f44c2b536643efe094bda90c4fc/href">https://medium.com/media/6d038f44c2b536643efe094bda90c4fc/href</a></iframe><p>The complexity of creating an account (.createAccount) is hidden from the Voter and Admin hence <strong>Abstraction.</strong></p><p>The .searchEvent , .viewresults and some of the other methods can take any form depending on where they are used, either in Admin, Voter, or the User constructor itself, hence depicting <strong>Polymorphism. </strong>For example, say we want the .viewResults method in the Voter to give a different response from that of the Admin we can do this</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/685407aa881c8f1b477909fa2a1782b7/href">https://medium.com/media/685407aa881c8f1b477909fa2a1782b7/href</a></iframe><p>This explains <strong>Polymorphism (</strong>the same method, .viewResults,giving a different response).</p><p>Yea yea! We are done. Now let’s create an index.js file so we call all our methods.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2685a59b433b00a599aa8ec825821093/href">https://medium.com/media/2685a59b433b00a599aa8ec825821093/href</a></iframe><p>Other features not implemented here are: autoincrement user ID, test with jest. Can be found on my <a href="https://github.com/whitehox/pollark-model">GitHub repo</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c810640fdc62" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Test Driven Development in Javascript]]></title>
            <link>https://codebarz.medium.com/test-driven-development-in-javascript-7002eef07974?source=rss-5e3ab1af7c25------2</link>
            <guid isPermaLink="false">https://medium.com/p/7002eef07974</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[test-driven-development]]></category>
            <category><![CDATA[jest]]></category>
            <dc:creator><![CDATA[Tega Oke]]></dc:creator>
            <pubDate>Sun, 02 May 2021 10:28:40 GMT</pubDate>
            <atom:updated>2023-07-15T22:04:39.276Z</atom:updated>
            <content:encoded><![CDATA[<p>Sequel to the Modelling a Polling System with Prototype Oriented Programming in Javascript, I am going to explain Test Driven Development in Javascript using that same model.</p><p>But before we get in that, let&#39;s explain testing.</p><p>Testing basically is a method by which software codes (either individual units or sets) are tested to determine if they are fit for use.</p><p>Test Driven Development, on the other hand, is a system of software development that requires a repetition a simple development of a cycle</p><p>There are many <a href="https://www.guru99.com/software-testing-introduction-importance.html">types of testing</a>, unit testing, integration testing, regression testing and so on, as a matter of fact, there is a whole lot of testing type.</p><ol><li>Unit Testing: Testing every single unit (function) of software against the functional requirements</li><li>Integration Testing: Testing integrated units of the software</li><li>End-to-End Testing: Testing to check the flow of software from beginning to end as a single application.</li></ol><p>But this article is not going in-depth into the types of testing but would introduce you to testing (basically unit testing) in Javascript.</p><h4><strong>Testing Framework</strong></h4><p>In Javascript, they are various testing frameworks available in Javascript some of which are <a href="http://www.jsunit.net/">JSUnit</a>, <a href="http://unitjs.com/">Unit.js</a>, <a href="https://qunitjs.com/">QUnit</a>, <a href="https://jasmine.github.io/">Jasmin</a>, <a href="https://karma-runner.github.io/2.0/index.html">Karma</a>, <a href="https://mochajs.org/">Mocha</a>, <a href="https://github.com/avajs">AVA</a>, <a href="https://jestjs.io/">Jest</a>. In this article, we are going to be using Jest which is a testing framework by Facebook.</p><p>Jest is a testing framework that provides a zero-configuration testing experience (all you need to do is run a command to install), does not require any library or external dependencies to work, and of course, can work without internet.</p><p>Let’s get started. We are going to test all functionality in our Polling System Model but first, we would need to set up our environment for testing.</p><p><strong>Prerequisites</strong></p><p>To set up our environment for test, we would need to run these commands in our terminal or command prompt for windows</p><ul><li>Install <a href="https://nodejs.org/en/">node</a> if you haven’t and initialize project for node</li></ul><pre>npm init</pre><p>This would require you to input some information but to get default values you can run</p><pre>npm init -y</pre><p>A package.json file containing the following would be generated.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2115701dd19c561ded1e04e3975443f3/href">https://medium.com/media/2115701dd19c561ded1e04e3975443f3/href</a></iframe><ul><li>Install <a href="https://jestjs.io/">jest</a></li></ul><pre>npm install --save-dev jest</pre><p>This would generate a node_modules directory, package-lock.json , and update package.json to have jest as a dependency. But we need our functions to be tested with Jest so we change our test in package.json to use jest</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ff793bc21ec512f48a4c7bb426d029cf/href">https://medium.com/media/ff793bc21ec512f48a4c7bb426d029cf/href</a></iframe><h4>How Test Works</h4><p>Before we get into testing our Polling System, let’s write a test a simple function so you get a feel on how testing works</p><p>We are going to create a simple function that returns a users username. The usual Test Driven Development practice is to write the test before the function or method we are testing so let’s get into that.</p><p>First, we have to think of all possible scenarios that a user would do, like, errors in the form of entering integers or not entering anything at all, to entering the right details for username.</p><p>We create a file for our test but the file extension used to save this file is sort of different. Here is how we save our test file, filename.test.js .</p><p>After doing all that, we write a test to cater for those scenarios.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb95acee991e43e5277dddf2a84a1c24/href">https://medium.com/media/eb95acee991e43e5277dddf2a84a1c24/href</a></iframe><p>Let’s talk about what’s happening in the test code above.</p><p>First, the <strong>describe</strong> keyword. The describe simply describes the suite of test cases enumerated by the “<strong>it</strong>” functions (in other words it tells us what the code in the describe block is trying to test). That brings us to the “<strong>it” </strong>keyword which is used to explain what the test should check for.</p><p>The <strong>expect </strong>there returns an expectation object (returns the response gotten from calling the function) which we call a <a href="https://jestjs.io/docs/en/using-matchers"><strong>matcher</strong></a> on.</p><h4><strong>Matchers — toBe, toEqual, toBeUndefined …</strong></h4><p>Matchers are used for checking if what we expect() from calling our functions with certain values correspond to what should be actually returned by the function.</p><h4><strong>Running the test</strong></h4><p>After writing our test code, we run on our terminal to see if it actually works. We run our test by using a command on the terminal</p><pre>npm test</pre><p>When we run this command, jest looks through our working directory for any file having the .test.js extension (the reason for saving test files as filename.test.js ). It is expected that all test cases should fail because we have not written our function. We get a response on the terminal that looks like</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bmOjFu0JQtlEMq7Pxqkl6g.png" /><figcaption>Response after running on the terminal</figcaption></figure><p>If you look at the <strong>ReferenceError </strong>you see we have a response saying the function we <strong>expect</strong> our response from is not defined. So, let’s write the function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/58d3d836000614facab8564ffa406006/href">https://medium.com/media/58d3d836000614facab8564ffa406006/href</a></iframe><p>If we run our code we still get the same error because we have not required our getUsername function in the test file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7f311ebe7b2b91c88b7774c6b94073a/href">https://medium.com/media/f7f311ebe7b2b91c88b7774c6b94073a/href</a></iframe><p>If we run our test again, we should get the following</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*760mWX-ncJZZL9YVFSRvbg.png" /><figcaption>Successful</figcaption></figure><p>Now, let’s change one of our test cases to expect something else, you can change anyone you like</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vPQqCNrzRJe5lXYAHKT15A.png" /><figcaption>A test case failed</figcaption></figure><p>Our test case for test integers failed. Why? If we look closely at the responsejest is giving. It says that the expected value and the received values are not the same.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ahAQgDPj8zRse25Ii3bliA.png" /></figure><p>They seem to be the same but they are not, I changed the uppercase “K” in the toBe() to a lowercase “k”. This just shows that the expected values are case sensitive. If we change back to uppercase “K” it would pass. Congratulations! You just tested your code.</p><h4><strong>Code Coverage</strong></h4><p>Jest also has a way of checking code coverage. This simply means check what part of your code has been tested.</p><p>To do this, we have to run a command</p><pre>npx jest --coverage</pre><p>This would add a coverage folder to the project directory and give a different response than our normal jest command</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BvqwL_c5qTdanTpUsm8dfg.png" /><figcaption>Code coverage</figcaption></figure><p>Jest gave the response with a table showing how many percents of our statements (if/else), function, lines of codes were covered (tested). We can get a better view of this in the browser by open the coverage folder, then the lcov-report, then open the index.html file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/697/1*P4K3lKYOSVWkx3L4hKFWMg.png" /></figure><p>We get a good view o four test coverage. Something that looks like</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KGD9KVFWBa79sAl2SoZLzg.png" /><figcaption>Test coverage with jest</figcaption></figure><p>Say we remove a test case from username.test.js and run our test coverage, we should get something like</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*By5TwqukZwqr1W0VlPA1PQ.png" /><figcaption>Some statements were not tested</figcaption></figure><p>We only have one function so it tells us we tested 100% of our function but 50% of branches (basically code parts in our function block). Now let open our test by clicking on the arts.js .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yteFZDt5Rtdf6Rj3rpWlvg.png" /><figcaption>The red part was not tested</figcaption></figure><p>The part highlighted in red tells us what part of our code was not tested. So let’s return the test case and run again</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-I-Wjlh-hwSMi2zJddxAqw.png" /><figcaption>all parts tested with jest</figcaption></figure><p>100% of our function was tested.</p><p>Understand that getting 100% test coverage is nice but not a must. The moment you begin to adjust the code to get 100% (removing statements) it becomes bad practice. So, think of all possible scenarios before writing the test.</p><h4><strong>Testing The Polling System Model</strong></h4><p>You can clone the <a href="https://github.com/whitehox/pollark-model">GitHub repo</a> and try to write a test for it, Should be fairly easy cause we already have written code for it and know what to expect, or you could just create another simple function to test.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/646e7815d6bec9d2a29eddeacbe0cf2a/href">https://medium.com/media/646e7815d6bec9d2a29eddeacbe0cf2a/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f36b2ec0f37ae0eb0cdd6aabb07d2f0/href">https://medium.com/media/2f36b2ec0f37ae0eb0cdd6aabb07d2f0/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d445badeb81beea3801c34ae1b35395/href">https://medium.com/media/0d445badeb81beea3801c34ae1b35395/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wAB2dleJw53MBpzztCguFw.png" /></figure><p>Finally, we tested our Polling System model.</p><p>All code can be found on my <a href="https://github.com/whitehox/pollark-model">github repo</a>. Thanks.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7002eef07974" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>