Skip to content

Commit

Permalink
Adding Cypress + Unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
brandoncorbin committed Jul 26, 2019
1 parent c6639dc commit 01a7458
Show file tree
Hide file tree
Showing 21 changed files with 418 additions and 94 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ Blockstack.org is a "Decentralized computing network and app ecosystem" - basica
/v01/data/trackers.json - trackers
/v01/data/boards.json - users board configuration

# Current Tracker Types

**tick** Single Tap

Just tap the button to automatically track, no value input.

**value** Numeric

Manually enter a value. Great for tracking caffiene, medicines

**range** Range

Great for any type of range selection - for example Mood.

**timer** Timer

Tap to start, tap to stop. The timer is great for tracking durations.

# Coding Rules

- **Keep it readable** - focus on writing code that new people can easily understand and follow. If the code can't do it, then do it with comments. There's no such thing as too much commenting.
Expand Down
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# TODO

Generated Thu Jul 25 2019 7:23 pm
Generated Fri Jul 26 2019 4:58 pm

- **src/store/user.js**
- TODO: Look at push notifications in the browser
Expand Down
1 change: 1 addition & 0 deletions cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
5 changes: 5 additions & 0 deletions cypress/fixtures/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Using fixtures to represent data",
"email": "[email protected]",
"body": "Fixtures are a great way to mock data for responses to routes"
}
29 changes: 29 additions & 0 deletions cypress/integration/app/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// <reference types="Cypress" />

context('App', () => {
beforeEach(() => {
cy.visit('http://localhost:5000');
});

it('cy.hash() - get the current URL hash', () => {
// https://on.cypress.io/hash
cy.hash().should('be.empty');
});
it('next button', () => {
cy.get('.footer-buttons > .btn')
.contains('Next')
.click();

cy.get('.footer-buttons > .btn')
.contains('Next')
.click();

cy.get('.footer-buttons > .btn')
.contains('Next')
.click();

// cy.get('.footer-buttons > .btn')
// .contains('Login/Register')
// .click();
});
});
43 changes: 43 additions & 0 deletions cypress/integration/modules/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import Log from '../../../src/modules/nomie-log/nomie-log';

describe('modules/nomie-log', function() {
let log;
let stub = {
note: `I'm a note for #testing and I'm #good(344) and #good and kinda #bad sometimes #bad`,
};

it('log initializes', () => {
log = new Log(stub);
expect(log).to.be.instanceOf(Log);
expect(log.note).to.equal(stub.note);
});

it('log.toObject', () => {
log = new Log(stub);
expect(log.toObject()._id).to.be.a('string');
});
it('log.expanded', () => {
log = new Log(stub);
log.expanded();
expect(log.trackers.testing.value).to.equal(1);
});
it('log.hasTracker', () => {
log = new Log(stub);
log.expanded();
expect(log.hasTracker('testing')).to.equal(true);
expect(log.hasTracker('nothing')).to.equal(false);
});
it('log.trackersArray', () => {
log = new Log(stub);
log.expand();
let trackers = log.trackersArray();
expect(trackers.length).to.equal(3);
});
it('log.addTag', () => {
log = new Log(stub);
log.expanded();
log.addTag('cheese', 44);
expect(log.hasTracker('cheese')).to.equal(true);
expect(log.trackers.cheese.value).to.equal(44);
});
});
78 changes: 78 additions & 0 deletions cypress/integration/modules/stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import Stats from '../../../src/modules/stats/stats';
import NomieLog from '../../../src/modules/nomie-log/nomie-log';
import Tracker from '../../../src/modules/tracker/tracker';
import dayjs from 'dayjs';

// TODO: write tests for mean math trackers

describe('modules/nomie-log', function() {
let rows = [
new NomieLog({
note: `I'm the #first #note and I'm #good(10)`,
}),
new NomieLog({
note: `I'm the #middle #note and I'm #good(104) too`,
end: dayjs()
.subtract(1, 'day')
.toDate()
.getTime(),
}),
new NomieLog({
note: `I'm the #last #note and I'm #bad and #good(1)`,
}),
new NomieLog({
note: `I'm a thing`,
}),
];

let stats = new Stats(rows, new Tracker({ tag: 'good' }));

it('stats initializes', () => {
expect(stats).to.be.instanceOf(Stats);
});
it('stats rows should expand', () => {
expect(stats.rows[0].trackers.first.value).to.equal(1);
});
it('results.year.count', () => {
expect(stats.results.year.count).to.equal(3);
});
it('results.month.count', () => {
expect(stats.results.month.count).to.equal(3);
});
it('results.day.count', () => {
expect(stats.results.day.count).to.equal(2);
});
it('getValueMap', () => {
let todayKey = dayjs().format('YYYY-MM-DD');
let yesterdayKey = dayjs()
.subtract(1, 'day')
.format('YYYY-MM-DD');
let valueMap = stats.getValueMap(rows);
expect(valueMap[todayKey]).to.be.instanceOf(Array);
expect(valueMap[todayKey]).to.include.members([10, 1]);
expect(valueMap[yesterdayKey]).to.include.members([104]);
});

it('getMinMaxFromValueMap()', () => {
let valueMap = stats.getValueMap(rows);
expect(stats.getMinMaxFromValueMap(valueMap).min.value).to.equal(11);
expect(stats.getMinMaxFromValueMap(valueMap).max.value).to.equal(104);
});

it('getRows - day', () => {
// number of days the tag shows up in the day
expect(stats.getRows('day').length).to.equal(2);
});

it('getRows - month', () => {
// number of days the tag shows up in the month
expect(stats.getRows('month').length).to.equal(3);
});

it('getChartData - month', () => {
// number of days the tag shows up in the month
expect(stats.toChartData('month').labels[0]).to.equal('01');
// Check today's value
expect(stats.toChartData('month').points[new Date().getDate() - 1].y).to.equal(11);
});
});
39 changes: 39 additions & 0 deletions cypress/integration/modules/tracker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Tracker from '../../../src/modules/tracker/tracker';

describe('modules/tracker', function() {
let tracker;
let stub = {
tag: 'tag',
label: 'Tester',
uom: 'mile',
emoji: '🥪',
type: 'value',
color: '#555555',
default: 10,
math: 'mean',
};

it('tracker initializes', () => {
tracker = new Tracker(stub);
expect(tracker).to.be.instanceOf(Tracker);
expect(tracker.label).to.equal(stub.label);
expect(tracker.tag).to.equal(stub.tag);
expect(tracker.emoji).to.equal(stub.emoji);
expect(tracker.type).to.equal(stub.type);
expect(tracker.default).to.equal(stub.default);
expect(tracker.color).to.equal(stub.color);
expect(tracker.uom).to.equal(stub.uom);
expect(tracker.math).oneOf(['sum', 'mean']);
// expect(math.sum([1, 2])).to.equal(3);
});
it('tracker.displayValue', () => {
tracker = new Tracker(stub);
expect(tracker.displayValue(100)).to.equal('100mi');
// expect(math.sum([1, 2])).to.equal(3);
});
it('tracker.toTag', () => {
tracker = new Tracker(stub);
expect(tracker.toTag('Brandon Is Good !!!')).to.equal('brandon_is_good');
// expect(math.sum([1, 2])).to.equal(3);
});
});
15 changes: 15 additions & 0 deletions cypress/integration/utils/extract-trackers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import extractor from '../../../src/utils/extract-trackers/extract-trackers';

describe('utils/extract-trackers', function() {
it('extract-tracker', () => {
let note = 'This is a #test of Nomies #extractor(4) and #cheese #cheese #cheese';
let results = extractor(note);

expect(results.test.tracker).to.equal('test');
expect(results.test.value).to.equal(1);
expect(results.extractor.value).to.equal(4);
expect(results.cheese.value).to.equal(3);

// expect(math.sum([1, 2])).to.equal(3);
});
});
43 changes: 43 additions & 0 deletions cypress/integration/utils/math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import math from '../../../src/utils/math/math';

describe('utils/math', function() {
it('math.sum', () => {
expect(math.sum([1, 2])).to.equal(3);
});
it('math.average', () => {
expect(math.average([30, 344, 21, 23])).to.equal(104.5);
});
it('math.round - default', () => {
expect(math.round(10.12345)).to.equal(10.12);
});
it('math.round - 1000', () => {
expect(math.round(10.12345, 1000)).to.equal(10.123);
});
it('math.round - 10', () => {
expect(math.round(10.12345, 10)).to.equal(10.1);
});
it('math.max', () => {
expect(math.max([0, 12, 223, 332, 12, 345, 32.32, -324])).to.equal(345);
});
it('math.min - negative', () => {
expect(math.min([0, 12, 223, 332, 12, 345, 32.32, -324, 443])).to.equal(-324);
});
it('math.min - no negative', () => {
expect(math.min([1, 12, 223, 332, 12, 345, 32.32, 443])).to.equal(1);
});
it('math.percentage', () => {
expect(math.percentage(100, 50)).to.equal(50);
});
it('math.random', () => {
expect(math.random([10, 20, 30])).to.be.oneOf([10, 20, 30]);
});
it('math.random_range', () => {
expect(math.random_range(0, 100)).to.be.within(0, 100);
});
it('math.percentile', () => {
expect(math.percentile([0, 50, 100])).to.include.members([0, 50, 100]);
});
it('math.trustfulNumber', () => {
expect(math.trustfulNumber(32 + 'abc', 0)).to.equal(0);
});
});
10 changes: 10 additions & 0 deletions cypress/integration/utils/regex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import regexs from '../../../src/utils/regex';

describe('utils/regex', function() {
it('extract tags by regex', () => {
let note = 'This is a #test my #friend(44) # taco';
let exp = new RegExp(regexs.tag, 'gi');

expect(note.match(exp)).to.include.members(['#test', '#friend(44)']);
});
});
27 changes: 27 additions & 0 deletions cypress/integration/utils/time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import time from '../../../src/utils/time/time';

describe('utils/time', function() {
it('padTime', () => {
expect(time.padTime(1)).to.equal('01');
});
it('secondsToTime', () => {
expect(time.secondsToTime(60 * 60)).to.equal('1:00:00');
expect(time.secondsToTime(10 * 60 * 60)).to.equal('10:00:00');
});
it('msToSecond', () => {
expect(time.msToSecond(1000)).to.equal(1);
});
it('timestringToSeconds', () => {
expect(time.timestringToSeconds('01:00:00')).to.equal(3600);
expect(time.timestringToSeconds('01:00:05')).to.equal(3605);
expect(time.timestringToSeconds('02:00:00')).to.equal(7200);
});
it('unitsToSeconds', () => {
expect(time.unitsToSeconds(1, 0, 0)).to.equal(3600);
expect(time.unitsToSeconds(1, 0, 5)).to.equal(3605);
expect(time.unitsToSeconds(2, 0, 0)).to.equal(7200);
});
it('getNumberedArray', () => {
expect(time.getNumberedArray(2)).to.include.members(['00', '01', '02']);
});
});
17 changes: 17 additions & 0 deletions cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)

module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
}
25 changes: 25 additions & 0 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add("login", (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This is will overwrite an existing command --
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
20 changes: 20 additions & 0 deletions cypress/support/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ***********************************************************
// This example support/index.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import './commands'

// Alternatively you can use CommonJS syntax:
// require('./commands')
Loading

0 comments on commit 01a7458

Please sign in to comment.