JSFiddle - React, Tailwind, and code Playground

JSFiddle ~1 min read
View original
Crawl details
  • best

This article is too short to summarise.

by Fagner Brack

HTML

<script src="https://code.jquery.com/qunit/qunit-2.0.1.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.0.1.css">
<div id="qunit"></div>

JavaScript

const fetchAccountFromNetwork = (accountNumber) => {
	let responseBodyForAccountRequest;
  if (accountNumber === 123) {
    responseBodyForAccountRequest = {
    	address: {
      	name: 'John',
        surname: 'Doe',
        street: 'George St',
        number: '483',
        suburb: 'Sydney',
        state: {
          abbreviatedName: 'NSW',
          name: 'New South Wales'
        }
      }
  	};
  }
  return Promise.resolve(responseBodyForAccountRequest);
};

const fetchFormattedAddressName = (
  accountNumber,
  fetchAccountDetails = fetchAccountFromNetwork
) => {
  return fetchAccountDetails(accountNumber).then((accountDetails) => {
    with(accountDetails.address) {
      return `${street} ${number} ${suburb}`;
    }
  });
};

QUnit.module('Fetching the account address');

QUnit.test('retrieves the formatted address', (assert) => {
  fetchFakeAccount = () => {
    return Promise.resolve({
    	address: {
        street: 'George St',
        number: '483',
        suburb: 'Sydney'
      }
  	});
  };
  const anyAccount = 123
  return fetchFormattedAddressName(
    anyAccount,
    fetchFakeAccount
  ).then((formattedAddress) => {
    const expected = 'George St 483 Sydney';
    const actual = formattedAddress;
    assert.strictEqual(expected, actual);
  });
});