Skip to content

Commit

Permalink
Merge pull request #17 from sampocs/pochyly
Browse files Browse the repository at this point in the history
more metrics testing, settings reducer testing, hashed email
  • Loading branch information
sampocs authored Apr 20, 2019
2 parents 294b000 + ba6a4d9 commit e05fbbb
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 90 deletions.
1 change: 0 additions & 1 deletion App.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import { Platform, StatusBar, StyleSheet, View } from 'react-native';
import AppNavigator from './navigation/AppNavigator';

import { createStore } from 'redux'
import { Provider } from 'react-redux';
import mainReducer from './reducers/mainReducer.js'
Expand Down
114 changes: 57 additions & 57 deletions helpers/__tests__/metrics-test.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,76 @@
import {
getPreviewMetrics
getPreviewMetrics,
getCurrentStreak
} from '../../helpers/metricsOperations'
import { getYearAgo } from '../../helpers/dateOperations';
import { getCurrentDate, getWeekAgo } from '../../helpers/dateOperations';
import { ALL_DATES_LIST } from '../../constants/Constants'
import testingDataWeekMonth from '../../assets/data/testingDataWeekMonth';

let completedIndex = [0, 1, 7, 9, 10, 11, 14, 18, 20, 21, 25]
let previewHistory = testingDataWeekMonth.history

let today = "2019-04-01"
let end = "2019-03-31"
let older = "2019-02-31"
let dateRange = ALL_DATES_LIST.filter((date) => (date >= older && date <= end)).sort().reverse()
let habitName = 'read'
let history = {}
let history2 = {}
let dow = 0
for (i in dateRange) {
let date = dateRange[i]
history[date] = {}
history2[date] = {}
dow = (dow + 1) % 7
if (dow != 5 && dow != 6) {
history[date][habitName] = {}
i = parseInt(i)
if (i === 0 || i === 1 || i === 7 || i === 9 ||
i === 10 || i === 11 || i === 14 || i === 18 ||
i === 20 || i === 11 || i === 25) {
history[date][habitName].completed = true
}
else {
history[date][habitName].completed = false
}
let streak1 = {}
let streak2 = {}
let streak3 = {}
let streak4 = {}
let streakDates = ALL_DATES_LIST.filter((date) => (date > getWeekAgo(getCurrentDate())) && (date <= getCurrentDate())).sort().reverse()
for (i in streakDates) {
let date = streakDates[i]
if (i === "0") {
streak1[date] = {workout: {completed: true}}
streak2[date] = {workout: {completed: true}}
streak3[date] = {workout: {completed: false}}
streak4[date] = {workout: {completed: false}}
}
else if (i === "1") {
streak1[date] = {workout: {completed: false}}
streak2[date] = {workout: {completed: true}}
streak3[date] = {workout: {completed: true}}
streak4[date] = {workout: {completed: false}}
}
else if (i === "2") {
streak1[date] = {workout: {completed: false}}
streak2[date] = {workout: {completed: false}}
streak3[date] = {workout: {completed: true}}
streak4[date] = {workout: {completed: true}}
}
else {
streak1[date] = {workout: {completed: false}}
streak2[date] = {workout: {completed: false}}
streak3[date] = {workout: {completed: false}}
streak4[date] = {workout: {completed: false}}
}
}
console.log(history)

let yearAgo = getYearAgo()
describe('Metrics', () => {
test('Metrics', () => {
expect(getPreviewMetrics(history, habitName, date=today)).toEqual({
test('PreviewMetrics', () => {
expect(getPreviewMetrics(previewHistory, 'workout')).toEqual({
weekly: {
totalDays: 6,
daysCompleted: 3,
percentage: '50%'
totalDays: 7,
daysCompleted: 5,
percentage: '71%'
},
monthly: {
totalDays: 23,
daysCompleted: 7,
percentage: '30%'
totalDays: 31,
daysCompleted: 22,
percentage: '71%'
},
yearly: {
totalDays: 23,
daysCompleted: 7,
percentage: '30%'
totalDays: 31,
daysCompleted: 22,
percentage: '71%'
}
})
})

test('CurrentStreak', () => {

expect(getCurrentStreak(streak1, 'workout')).toEqual(1)

expect(getCurrentStreak(streak2, 'workout')).toEqual(2)

expect(getCurrentStreak(streak3, 'workout')).toEqual(2)

expect(getCurrentStreak(streak4, 'workout')).toEqual(0)

expect(getPreviewMetrics(history2, habitName, date=today)).toEqual({
weekly: {
totalDays: 0,
daysCompleted: 0,
percentage: '0%'
},
monthly: {
totalDays: 0,
daysCompleted: 0,
percentage: '0%'
},
yearly: {
totalDays: 0,
daysCompleted: 0,
percentage: '0%'
}
})
})
})
19 changes: 0 additions & 19 deletions helpers/metricsOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,25 +275,6 @@ export const getBarChart = (history, type, habitName) => {
return (monthPoints.reduce((sum_, i) => sum_ + i, 0) / monthPoints.length)
})

// if (weekData.length < 7) {
// for (i = 0; i < 7 - weekData.length; i++) {
// weekData = [0, ...weekData]
// xLabelsWeek = [, ...xLabelsWeek]
// }
// }
// if (monthData.length < 31) {
// for (i = 0; i < 31 - monthData.length; i++) {
// monthData = [0, ...monthData]
// xLabelsMonth = [, ...xLabelsMonth]
// }
// }
// if (yearData.length < 365) {
// for (i = 0; i < 365 - yearData.length; i++) {
// yearData = [0, ...yearData]
// xLabelsYear = [, ...xLabelsYear]
// }
// }

return {
weekly: {
data: weekData,
Expand Down
15 changes: 13 additions & 2 deletions helpers/miscHelpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
export const emailToFirebaseRef = (email) => {
return emailRef = email.replace('.', '_')
}
return hashFunction(email)
}

const hashFunction = (string) => {
let hash = 0, i, chr;
if (string.length === 0) return hash;
for (i = 0; i < string.length; i++) {
chr = string.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0;
}
return hash.toString();
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"android": "expo start --android",
"ios": "expo start --ios",
"eject": "expo eject",
"test": "node ./node_modules/jest/bin/jest.js --watchAll --coverage"
"test": "node ./node_modules/jest/bin/jest.js --watchAll"
},
"jest": {
"preset": "jest-expo"
Expand Down
81 changes: 74 additions & 7 deletions reducers/__tests__/settingsReducer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import settingsReducer from '../settingsReducer'
import {
ADD_HABIT_TO_SETTINGS,
RESTORE_SETTINGS_FROM_FIREBASE,
DELETE_HABIT_FROM_SETTINGS
DELETE_HABIT_FROM_HABIT_SETTINGS,
DELETE_HABIT_FROM_HABIT_ORDER,
CHANGE_HABIT_ORDER
} from '../../actions/actions'
import Constants from '../../constants/Constants'

Expand All @@ -25,7 +27,8 @@ describe('Settings Reducer', () => {
habitInfo: {},
icon: 'dumbbells'
}
}
},
habitOrder: ['workout']
}, {
type: ADD_HABIT_TO_SETTINGS,
habitName: 'read',
Expand All @@ -45,7 +48,7 @@ describe('Settings Reducer', () => {
unit: "min",
goal: 60
},
icon: 'book'
icon: 'book',
}

})).toEqual({
Expand Down Expand Up @@ -83,7 +86,8 @@ describe('Settings Reducer', () => {
},
icon: 'book'
}
}
},
habitOrder: ['workout', 'read']
})
})

Expand Down Expand Up @@ -212,9 +216,10 @@ describe('Settings Reducer', () => {
},
icon: 'book'
}
}
},
habitOrder: ['workout', 'read']
}, {
type: ADD_HABIT_TO_SETTINGS,
type: DELETE_HABIT_FROM_HABIT_SETTINGS,
habitName: 'read',
})).toEqual({
habitSettings: {
Expand All @@ -233,7 +238,69 @@ describe('Settings Reducer', () => {
habitInfo: {},
icon: 'dumbbells'
},
}
},
habitOrder: ['workout', 'read']
})
})

test('DELETE_HABIT_FROM_HABIT_ORDER', () => {
expect(settingsReducer({
habitSettings: {
workout: {
disappearWhenCompleted: true,
daysOfWeek: [
false,
true,
true,
true,
true,
true,
false
],
type: Constants.COMPLETE,
habitInfo: {},
icon: 'dumbbells'
}
},
habitOrder: ['workout', 'read']
}, {
type: DELETE_HABIT_FROM_HABIT_ORDER,
habitName: 'read',
})).toEqual({
habitSettings: {
workout: {
disappearWhenCompleted: true,
daysOfWeek: [
false,
true,
true,
true,
true,
true,
false
],
type: Constants.COMPLETE,
habitInfo: {},
icon: 'dumbbells'
},
},
habitOrder: ['workout']
})
})

test('CHANGE_HABIT_ORDER', () => {
expect(settingsReducer({
habitSettings: {
},
habitOrder: ['workout', 'read', 'chores']
}, {
type: CHANGE_HABIT_ORDER,
prevOrder: ['2', '0', '1'],
nextOrder: ['0', '2', '1']
})).toEqual({
habitSettings: {
},
habitOrder: ['workout', 'chores', 'read']
})
})
})
2 changes: 1 addition & 1 deletion reducers/mainReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import initialData from "../assets/data/initialData";
import testingDataWeekMonth from "../assets/data/testingDataWeekMonth";

const initialState = testingDataWeekMonth
const initialState = initialData

function mainReducer(state = initialState, action) {
if (action.type === CLEAR_USER_DATA) {
Expand Down
5 changes: 3 additions & 2 deletions screens/AddHabitScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,14 +422,15 @@ class AddHabitScreen extends React.Component {
</View>
</View>

{/*|||||||||||| COMPLETE ACTION |||||||||||||||*/}
{/*|||||||||||| COMPLETE ACTION |||||||||||||||
<View style={styles.completionActionToggle}>
<DualToggle
color={Colors.calendarBlue}
labels={['Change Color', 'Disappear']}
setParentState={this.setCompletionActionToggle.bind(this)}
/>
</View>
</View>
*/}

{/*|||||||||||| ICON MODAL SCREEN |||||||||||||||*/}
<View style={styles.chooseIconButtonContainer}>
Expand Down

0 comments on commit e05fbbb

Please sign in to comment.