Skip to content

Commit 323433b

Browse files
committed
update parser tests
1 parent 5d34a94 commit 323433b

File tree

11 files changed

+140
-42
lines changed

11 files changed

+140
-42
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## [0.12.3] - WIP
6+
- improved page open. uses `onDidOpen` callback
7+
- improved documentation, tests
8+
59
## [0.12.2] - 2016-08-25
610
- drop "core-coderoad" dependency
711
- remove additional dependencies for a smaller footprint

src/modules/page/actions.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
import {hintPositionSet, routeSet, testLoad} from '../../actions';
22
import {PAGE_SET} from './types';
33

4+
/**
5+
* calls PAGE_SET with next page
6+
* @param {} any
7+
* @returns thunk
8+
*/
49
export function pageNext(): Redux.ThunkAction<any, any, {}> {
510
return (dispatch, getState): void => {
611
let {pagePosition} = getState();
712
dispatch(pageSet(pagePosition + 1));
813
};
914
}
1015

16+
/**
17+
* PAGE_SET action creator
18+
*
19+
* opens new page, sets hintPosition to 0, resets tasks
20+
* loads tests
21+
*
22+
* if final page, routes to final
23+
*
24+
* @param {} pagePosition=0
25+
* @returns thunk
26+
*/
1127
export function pageSet(pagePosition = 0): Redux.ThunkAction<any, any, {}> {
1228
return (dispatch, getState): void => {
1329
const state = getState();

src/modules/page/task-actions/handle-action-string.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const Type = {
1414
// parse task string for command/params
1515
export default function handleActionString(
1616
actionString: string
17-
): Promise<void> {
17+
): Promise<any> {
1818
return new Promise((resolve, reject) => {
1919
if (typeof actionString !== 'string') {
2020
reject(actionString);

src/modules/page/task-actions/handle-actions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import handleActionString from './handle-action-string';
22

3+
/**
4+
* call each task action in sequential order
5+
* @param {string[][]} actions
6+
* @returns void
7+
*/
38
export default function handleTaskActions(actions: string[][]): void {
49
const next = actions.shift();
510
if (next && next.length) {

src/modules/page/task-actions/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import handleTaskActions from './handle-actions';
33

44
// trigger actions only once, moving fowards
55
let taskPositionTracker = 0;
6-
6+
/**
7+
* task action reducer
8+
* @param {} t=[]
9+
* @param {Action} action
10+
* @returns string[][] array of array of actions
11+
*/
712
export default function taskActionsReducer(
813
t = [], action: Action
914
): string[][] {

src/modules/page/task-actions/parser.spec.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/// <reference path="../../../typings/globals/jest/index.d.ts" />
2+
3+
import parseParams from './parse-params';
4+
5+
describe('parseBreaks', function() {
6+
7+
describe('getParams', function() {
8+
it('should return the same string in an array if only one param', function () {
9+
let params = 'first';
10+
let parser = new parseParams();
11+
let breaks = parser.getParams(params);
12+
expect(breaks).toEqual(['first']);
13+
});
14+
15+
it('should return params in a simple string', function() {
16+
let params = 'first, second, third';
17+
let parser = new parseParams();
18+
let breaks = parser.getParams(params);
19+
expect(breaks).toEqual(['first', 'second', 'third']);
20+
});
21+
22+
it('should ignore breaks within an object', function() {
23+
let params = '{ a: 1, b: 2 }, second, third';
24+
let parser = new parseParams();
25+
let breaks = parser.getParams(params);
26+
expect(breaks).toEqual(['{ a: 1, b: 2 }', 'second', 'third']);
27+
});
28+
29+
it('should ignore breaks within an array', function() {
30+
let params = '[ a: 1, b: 2 ], second, third';
31+
let parser = new parseParams();
32+
let breaks = parser.getParams(params);
33+
expect(breaks).toEqual(['[ a: 1, b: 2 ]', 'second', 'third']);
34+
});
35+
36+
it('should ignore breaks within brackets', function() {
37+
let params = 'function (a, b) {}, second, third';
38+
let parser = new parseParams();
39+
let breaks = parser.getParams(params);
40+
expect(breaks).toEqual(['function (a, b) {}', 'second', 'third']);
41+
});
42+
});
43+
44+
});

src/modules/page/task-actions/parser.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import ParseParams from './parse-params';
22

3+
/**
4+
* gets the command from the front of an action
5+
* example: open('file.js') -> open
6+
* @param {string} actionString
7+
* @returns string action command
8+
*/
39
export function getCommand(actionString: string): string {
410
// content before bracket
511
let command = actionString.substring(0, actionString.indexOf('('));
@@ -10,6 +16,12 @@ export function getCommand(actionString: string): string {
1016
return command;
1117
}
1218

19+
/**
20+
* gets the params from an action
21+
* example: open('file.js') -> file.js
22+
* @param {string} actionString
23+
* @returns string action params
24+
*/
1325
export function getParams(actionString: string): string[] {
1426
// content in brackets, split by comma
1527
let parser = new ParseParams();
@@ -23,6 +35,11 @@ export function getParams(actionString: string): string[] {
2335
return paramsList;
2436
}
2537

38+
/**
39+
*
40+
* @param {string} text
41+
* @returns Object
42+
*/
2643
function createObjectFromKeyValString(text: string): Object {
2744
let keyValList: string[] = text.split(/[:,]/);
2845
let obj = {};
@@ -41,6 +58,11 @@ function createObjectFromKeyValString(text: string): Object {
4158
return obj;
4259
}
4360

61+
/**
62+
* get options from an action string
63+
* @param {string} paramString
64+
* @returns Object
65+
*/
4466
export function getOptions(paramString: string): { param: string, options: Object } {
4567
let hasOptions = paramString.match(/\{(.+)?\}/);
4668
let options = {};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path="../../../typings/globals/jest/index.d.ts" />
2+
3+
import reducer from './index';
4+
5+
describe('task-action reducer', () => {
6+
7+
// it('does nothing if no known action is called', () => {
8+
// const action = { type: 'unknown' };
9+
// expect(reducer([], action)).toEqual([]);
10+
// });
11+
12+
it('demo', () => {
13+
expect(true).toBe(true);
14+
});
15+
16+
});

src/modules/page/task-tests/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@ import {readFileSync} from 'fs';
22

33
import {PAGE_SET} from '../types';
44

5+
/**
6+
* task test reducer
7+
* @param {} taskTests=''
8+
* @param {Action} action
9+
* @returns string
10+
*/
511
export default function taskTestsReducer(
612
taskTests = '', action: Action
713
): string {
814
switch (action.type) {
915

1016
case PAGE_SET:
11-
const {tutorial, tasks} = action.payload;
17+
const {tasks} = action.payload;
18+
1219
// map over task tests from coderoad.json
1320
return [].concat.apply([], tasks.map(
1421
task => task.tests || []
@@ -20,7 +27,7 @@ export default function taskTestsReducer(
2027
} catch (e) {
2128
console.log('Error reading test file', e);
2229
}
23-
// return concatted test files
30+
// return concatenated test files
2431
return output;
2532
}, '');
2633

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path="../../../typings/globals/jest/index.d.ts" />
2+
3+
import reducer from './index';
4+
5+
describe('task tests reducer', () => {
6+
7+
it('does nothing if no known action', () => {
8+
const action = { type: 'unknown' };
9+
expect(reducer(undefined, action)).toBe('');
10+
});
11+
12+
// it('handles PAGE_SET', () => {
13+
// const tutorial = {};
14+
// const tasks = [];
15+
// });
16+
17+
});

0 commit comments

Comments
 (0)