<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Zain</title>
    <description>The latest articles on DEV Community by Zain (@zainzzkk).</description>
    <link>https://dev.to/zainzzkk</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F880602%2F8f1c9605-53de-4265-98ee-2d26c7c5ea2d.png</url>
      <title>DEV Community: Zain</title>
      <link>https://dev.to/zainzzkk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zainzzkk"/>
    <language>en</language>
    <item>
      <title>Cypress - counting the number of elements on a page</title>
      <dc:creator>Zain</dc:creator>
      <pubDate>Wed, 22 Jun 2022 16:22:12 +0000</pubDate>
      <link>https://dev.to/zainzzkk/cypress-counting-the-number-of-elements-on-a-page-aka</link>
      <guid>https://dev.to/zainzzkk/cypress-counting-the-number-of-elements-on-a-page-aka</guid>
      <description>&lt;p&gt;A scenario you may face is when you want to count how many of a certain element is on a page when using cypress.  There is a simple solution for this, especially if you use &lt;code&gt;data-cy&lt;/code&gt; in your code to ease testing.&lt;/p&gt;

&lt;p&gt;the command: &lt;code&gt;cy.get('[data-cy="element"').should('have.length', 2);&lt;/code&gt; should get you the number of a certain element on a page.&lt;/p&gt;

&lt;p&gt;If you do not use &lt;code&gt;data-cy&lt;/code&gt;'s, then finding by class should work: &lt;code&gt;cy.get('class_name').should('have.length', 2);&lt;/code&gt;.  Furthermore, if you want to find the number of elements in a table, you can do &lt;code&gt;cy.get('table_name').find('tr').should('have.length', 2);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;A more advanced way of finding elements can be done by this method: &lt;code&gt;cy.get('item:visible:contains("text")').should('have.length, 2);&lt;/code&gt; where you are using &lt;code&gt;contains&lt;/code&gt; to find all the items with a certain text you want.  The visible flag has to be used as &lt;code&gt;contains&lt;/code&gt; can only find visible DOM elements.&lt;/p&gt;

&lt;p&gt;Also, remember, you can simplify, without having to use &lt;code&gt;cy.get()&lt;/code&gt; each time, especially if running multiple tests by declaring your items in a file and importing them into your test file as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const page = {
   item: () =&amp;gt; cy.get('item'),
};

module.exports = page;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you can run &lt;code&gt;page.item().should('have.length', 2);&lt;/code&gt; which can be considered better practice.&lt;/p&gt;

&lt;p&gt;More information at &lt;a href="https://docs.cypress.io/guides/references/assertions#Common-Assertions"&gt;https://docs.cypress.io/guides/references/assertions#Common-Assertions&lt;/a&gt;&lt;/p&gt;

</description>
      <category>cypress</category>
      <category>testing</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Solve UK time changes (DST) with NodeJS and date-fns and Docker (epoch/unix format)</title>
      <dc:creator>Zain</dc:creator>
      <pubDate>Tue, 21 Jun 2022 20:37:23 +0000</pubDate>
      <link>https://dev.to/zainzzkk/solve-uk-time-changes-dst-with-nodejs-and-date-fns-and-docker-epochunix-format-2hok</link>
      <guid>https://dev.to/zainzzkk/solve-uk-time-changes-dst-with-nodejs-and-date-fns-and-docker-epochunix-format-2hok</guid>
      <description>&lt;p&gt;DST time changes can wreck havoc when in the UK time-zone, where you'll find your times might be one hour behind, especially when using epoch/unix format to save your timestamps.&lt;/p&gt;

&lt;p&gt;When using NodeJS with &lt;a href="https://date-fns.org/"&gt;date-fns&lt;/a&gt; in a docker container, this simple guide will show how to fix the issue.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;new Date();&lt;/code&gt; might return a time in GMT as compared to DST. Furthermore, using &lt;code&gt;getUnixTime()&lt;/code&gt; from &lt;a href="https://date-fns.org/v2.28.0/docs/getUnixTime"&gt;date-fns&lt;/a&gt; will return an epoch timestamp which will be in GMT.  Converting this back to BST may present a challenge.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { getUnixTime, format } = require("date-fns");

const date = new Date();
console.log('new Date() print',date, '\n');

const unixTime = getUnixTime(date);

const formatted = format(new Date(unixTime * 1000), 'h:mm aa', {
    timeZone: 'Europe/London',
});
console.log('formatted timestamp with timezone', formatted);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running the above code will give you a timestamp which takes DST into account for correct BST as shown by this screenshot from terminal.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--X7-pmz4m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o25nm1mp1a2x0hskggs7.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X7-pmz4m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o25nm1mp1a2x0hskggs7.JPG" alt="Image description" width="515" height="84"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You may be wondering about the &lt;code&gt;unixTime * 1000&lt;/code&gt;.  This is because date-fns &lt;code&gt;getUnixTime()&lt;/code&gt; gives a unix timestamp without milliseconds, and hence converts to seconds.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://date-fns.org/v2.28.0/docs/format"&gt;date-fns&lt;/a&gt; &lt;code&gt;format()&lt;/code&gt; function accepts a third parameter for time-zone which is why we have used &lt;code&gt;{ timeZone: 'Europe/London', }&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;If running in a docker container and it still returns a GMT timestamp, then adding &lt;code&gt;- TZ=Europe/London&lt;/code&gt; as part of the environment section of your docker-compose file will help solve the issue by setting the container time-zone to London.  Example below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test:
    build: ./test
    image: ....
    depends_on:
      - ....
    ports:
      - "1234:1234" # http
      - "1234:1234" # debug
    volumes:
      - ....
      - ....
      - ....
    environment:
      - PORT=....
      - TZ=Europe/London
    command: ....
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>docker</category>
      <category>timezone</category>
      <category>node</category>
    </item>
  </channel>
</rss>
