Skip to content

Commit e7a4b94

Browse files
committed
improved docs and commenting in setup & progress
1 parent b5178c1 commit e7a4b94

File tree

16 files changed

+176
-24
lines changed

16 files changed

+176
-24
lines changed

lib/modules/progress/reducer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"use strict";
22
var types_1 = require('./types');
33
var local_storage_1 = require('./utils/local-storage');
4-
var _progress = {
4+
exports._progress = {
55
completed: false,
66
pages: []
77
};
88
function progress(progress, action) {
9-
if (progress === void 0) { progress = _progress; }
9+
if (progress === void 0) { progress = exports._progress; }
1010
switch (action.type) {
1111
case types_1.PROGRESS_LOAD:
1212
var saved = local_storage_1.loadProgressFromLocalStorage(action.payload.tutorial);

lib/modules/progress/utils/local-storage.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
function getLocalStorageKey(tutorial) {
33
return 'coderoad:' + tutorial.name;
44
}
5+
exports.getLocalStorageKey = getLocalStorageKey;
56
function saveToLocalStorage(tutorial, progress) {
67
try {
78
window.localStorage
89
.setItem(getLocalStorageKey(tutorial), JSON.stringify(progress));
910
}
1011
catch (e) {
11-
console.log('Error saving progress:', e);
12+
throw new Error("Error saving progress. Invalid progress: " + progress + ". " + e);
1213
}
1314
}
1415
exports.saveToLocalStorage = saveToLocalStorage;

lib/modules/setup/Checks/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
"use strict";
22
var types_1 = require('../types');
33
var verify_1 = require('../utils/verify');
4-
var _checks = {
4+
exports._checks = {
55
passed: false,
66
system: {
77
node: false,
88
npm: false,
99
xcode: false,
10+
atom: false,
1011
},
1112
setup: {
1213
hasDir: false,
@@ -15,7 +16,7 @@ var _checks = {
1516
}
1617
};
1718
function checks(checks, action) {
18-
if (checks === void 0) { checks = _checks; }
19+
if (checks === void 0) { checks = exports._checks; }
1920
switch (action.type) {
2021
case types_1.SETUP_VERIFY:
2122
var _a = action.payload, dir = _a.dir, packageJson = _a.packageJson;
+5-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
"use strict";
22
var path_1 = require('path');
3-
var selectors_1 = require('../../../selectors');
4-
var packageData = "{\n \"name\": \"demo\",\n \"dependencies\": {\n \"coderoad-functional-school\": \"^0.2.2\"\n }\n}";
3+
var editor_1 = require('../../editor');
4+
var packageData = "{\n \"name\": \"demo\",\n \"version\": \"0.1.0\",\n \"private\": true,\n \"dependencies\": {\n \"coderoad-functional-school\": \"^1.1.3\"\n }\n}";
55
function createPackageJson(dir) {
66
var packagePath = path_1.join(dir, 'package.json');
77
return new Promise(function (resolve, reject) {
8-
selectors_1.open(packagePath);
8+
editor_1.open(packagePath);
99
setTimeout(function () { return resolve(); });
1010
}).then(function () {
11-
selectors_1.set(packageData);
11+
editor_1.set(packageData);
1212
});
1313
}
1414
exports.createPackageJson = createPackageJson;
1515
function openDirectory() {
16-
selectors_1.openFolder();
16+
editor_1.openFolder();
1717
}
1818
exports.openDirectory = openDirectory;

src/modules/progress/reducer.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path="../../typings/globals/jest/index.d.ts" />
2+
3+
import reducer, { _progress } from './reducer';
4+
5+
describe('progress reducer', () => {
6+
7+
it('should do nothing if no action is triggered', () => {
8+
const action = { type: 'unknown' };
9+
expect(reducer(undefined, action)).toEqual(_progress);
10+
});
11+
12+
});

src/modules/progress/reducer.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import {PROGRESS_COMPLETE_PAGE, PROGRESS_COMPLETE_TUTORIAL, PROGRESS_LOAD} from './types';
22
import {loadProgressFromLocalStorage, saveToLocalStorage} from './utils/local-storage';
33

4-
const _progress: CR.Progress = {
4+
export const _progress: CR.Progress = {
55
completed: false,
66
pages: []
77
};
88

9+
/**
10+
* Progress reducer saves local tutorial progress
11+
* @param {} progress=_progress
12+
* @param {Action} action
13+
* @returns CR.Progress
14+
*/
915
export default function progress(
1016
progress = _progress, action: Action
1117
): CR.Progress {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/// <reference path="../../../typings/globals/jest/index.d.ts" />
2+
/// <reference path="../../../typings/common/global.d.ts" />
3+
4+
import { getLocalStorageKey, saveToLocalStorage, loadProgressFromLocalStorage } from './local-storage';
5+
6+
describe('local storage function', () => {
7+
8+
const tutorial = {
9+
name: 'example'
10+
};
11+
12+
afterEach(() => {
13+
global.window = null;
14+
})
15+
16+
it('getLocalStorage creates a key based on the tutorial name', () => {
17+
const tutorial = {
18+
name: 'example'
19+
};
20+
expect(getLocalStorageKey(tutorial)).toBe('coderoad:example');
21+
});
22+
23+
it('saveToLocalStorage saves tutorial progress', () => {
24+
global.window.localStorage = {
25+
setItem: jest.fn()
26+
};
27+
const progress = [true, true, false];
28+
saveToLocalStorage(tutorial, progress);
29+
expect(global.window.localStorage.setItem).toBeCalledWith('coderoad:example', JSON.stringify(progress));
30+
});
31+
32+
it('saveToLocalStorage should throw an error if progress is invalid', () => {
33+
global.window.localStorage = {
34+
setItem: jest.fn()
35+
};
36+
const progress = 'invalid';
37+
expect(saveToLocalStorage(tutorial, progress)).toThrowError();
38+
});
39+
40+
it('loadProgressFromLocalStorage should load saved progress', () => {
41+
const progress = [true, true, false];
42+
global.window.localStorage = {
43+
getItem: key => JSON.stringify({ "completed": false, "pages": progress })
44+
};
45+
expect(loadProgressFromLocalStorage(tutorial)).toEqual({ "completed": false, "pages": progress });
46+
});
47+
48+
it('loadProgressFromLocalStorage should return null if no progress saved', () => {
49+
const progress = null;
50+
global.window.localStorage = {
51+
getItem: key => null
52+
};
53+
expect(loadProgressFromLocalStorage(tutorial)).toEqual(null);
54+
});
55+
56+
});

src/modules/progress/utils/local-storage.ts

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
1-
function getLocalStorageKey(tutorial: CR.Tutorial) {
1+
/**
2+
* create a key for saving progress in local storage
3+
* @param {CR.Tutorial} tutorial
4+
* @returns tutorial
5+
*/
6+
export function getLocalStorageKey(tutorial: CR.Tutorial): string {
27
return 'coderoad:' + tutorial.name;
38
}
49

10+
/**
11+
* save progress to local storage
12+
* @param {CR.Tutorial} tutorial
13+
* @param {CR.Progress} progress
14+
* @returns void
15+
*/
516
export function saveToLocalStorage(
617
tutorial: CR.Tutorial, progress: CR.Progress
7-
): void {
18+
): void|Error {
819
try {
920
window.localStorage
1021
.setItem(getLocalStorageKey(tutorial), JSON.stringify(progress));
1122
} catch (e) {
12-
console.log('Error saving progress:', e);
23+
throw new Error(`Error saving progress. Invalid progress: ${progress}. ${e}`);
1324
}
1425
}
15-
16-
export function loadProgressFromLocalStorage(tutorial: CR.Tutorial) {
17-
const savedProgress: CR.Progress = JSON.parse(
26+
/**
27+
* load progress from local storage
28+
* @param {CR.Tutorial} tutorial
29+
*/
30+
export function loadProgressFromLocalStorage(tutorial: CR.Tutorial): CR.Progress|null {
31+
const savedProgress = JSON.parse(
1832
window.localStorage.getItem(getLocalStorageKey(tutorial)) || null
1933
);
2034
if (savedProgress) {

src/modules/setup/checks/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import {SETUP_VERIFY} from '../types';
22
import setupVerify from '../utils/verify';
33

4-
const _checks: CR.Checks = {
4+
export const _checks: CR.Checks = {
55
passed: false,
66
system: {
77
node: false,
88
npm: false,
99
xcode: false,
10+
atom: false,
1011
},
1112
setup: {
1213
hasDir: false,
@@ -15,6 +16,12 @@ const _checks: CR.Checks = {
1516
}
1617
};
1718

19+
/**
20+
* setup and system checks reducer
21+
* @param {} checks=_checks
22+
* @param {Action} action
23+
* @returns CR.Checks
24+
*/
1825
export default function checks(
1926
checks = _checks, action: Action
2027
): CR.Checks {
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path="../../../typings/globals/jest/index.d.ts" />
2+
3+
import reducer, { _checks } from './index';
4+
5+
describe('checks reducer', () => {
6+
7+
it('does nothing if no action received', () => {
8+
const action = { type: 'unknown' };
9+
expect(reducer(undefined, action)).toEqual(_checks);
10+
});
11+
12+
});

src/modules/setup/package-json/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ import fileExists from 'node-file-exists';
66

77
const readParse = p => JSON.parse(readFileSync(p, 'utf8'));
88

9+
/**
10+
* package.json reducer
11+
* @param {} pj=null
12+
* @param {Action} action
13+
* @returns PackageJson
14+
*/
915
export default function packageJson(
1016
pj = null, action: Action
11-
): PackageJson {
17+
): PackageJson|null {
1218
switch (action.type) {
1319

1420
case SETUP_PACKAGE:
+13-5
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
import { join } from 'path';
2-
3-
import { open, openFolder, openTerminal, set } from '../../../selectors';
4-
import { setupVerify } from '../actions';
2+
import { open, openFolder, openTerminal, set } from '../../editor';
53

64
const packageData = `{
75
"name": "demo",
6+
"version": "0.1.0",
7+
"private": true,
88
"dependencies": {
9-
"coderoad-functional-school": "^0.2.2"
9+
"coderoad-functional-school": "^1.1.3"
1010
}
1111
}`;
1212

13+
/**
14+
* creates a basic package.json file in the users directory
15+
* @param {string} dir
16+
* @returns Promise
17+
*/
1318
export function createPackageJson(dir: string): Promise<void> {
1419
const packagePath = join(dir, 'package.json');
1520
return new Promise((resolve, reject) => {
1621
open(packagePath);
1722
setTimeout(() => resolve());
1823
}).then(() => {
1924
set(packageData);
20-
// store.dispatch(setupVerify());
2125
});
2226
}
2327

28+
/**
29+
* opens a directory
30+
* @returns void
31+
*/
2432
export function openDirectory(): void {
2533
openFolder();
2634
}

src/modules/setup/utils/action-system.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {setupVerify} from '../actions';
22
import commandLine from 'atom-plugin-command-line';
33

4+
// WIP
45
export function updateNpm(): void {
56
commandLine('npm', 'update -g npm')
67
.then((res) => {

src/modules/setup/utils/check-system.ts

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ export function nodeMinVersion(): Promise<boolean> {
5959
return minVersion('node', versions.node);
6060
}
6161

62+
/**
63+
* checks if is a mac
64+
* checks if xcode is installed
65+
* sets true if mac & !xcode, else false
66+
* @returns Promise
67+
*/
6268
export function requiresXCode(): Promise<boolean> | boolean {
6369
if (!navigator.platform.match(/Mac/)) {
6470
return true;

src/modules/setup/utils/verify.ts

+11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import {atomMinVersion, nodeMinVersion, npmMinVersion, requiresXCode} from './check-system';
22
import {tutorials} from 'coderoad-cli';
33

4+
/**
5+
* Returns true if all object key values are true
6+
* @param {Object} obj
7+
* @returns boolean
8+
*/
49
function allTrue(obj: Object): boolean {
510
return Object.values(obj).every(x => x === true);
611
}
712

13+
/**
14+
* verifies setup of system & project
15+
* @param {string} dir
16+
* @param {PackageJson} packageJson
17+
* @returns CR
18+
*/
819
export default function setupVerify(
920
dir: string, packageJson: PackageJson
1021
): CR.Checks {

src/typings/common/global.d.ts

+11
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,15 @@ declare var global: Global;
33
interface Global {
44
document: any;
55
atom: any;
6+
window: any;
67
}
8+
9+
10+
declare var window: Window;
11+
12+
interface Window {
13+
localStorage: {
14+
getItem: (item: any) => any;
15+
setItem: (item: any, value: any) => any;
16+
}
17+
}

0 commit comments

Comments
 (0)