Skip to content

Commit 5523192

Browse files
committed
feat(function-entry): added function entry
1 parent 78ad822 commit 5523192

File tree

5 files changed

+219
-13
lines changed

5 files changed

+219
-13
lines changed

packages/function-entry/configure/src/feedback-mapper.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ export const modelToFeedbackConfig = model => {
88
correctFeedbackType: correctFeedback.type || 'default',
99
incorrectFeedback: incorrectFeedback.value,
1010
incorrectFeedbackType: incorrectFeedback.type || 'default',
11-
}
12-
}
11+
};
12+
};
1313

1414
export const feedbackConfigToModel = (config, model) => {
1515

@@ -18,11 +18,12 @@ export const feedbackConfigToModel = (config, model) => {
1818
model.correctResponse.feedback = {
1919
type: config.correctFeedbackType,
2020
value: config.correctFeedback
21-
}
21+
};
2222

2323
model.incorrectFeedback = {
2424
type: config.incorrectFeedbackType,
2525
value: config.incorrectFeedback
26-
}
26+
};
27+
2728
return model;
28-
}
29+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"presets": [
3+
"env",
4+
"stage-0"
5+
]
6+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import { model } from '../src/index';
2+
3+
4+
describe('model', () => {
5+
6+
7+
let result, question, session, env;
8+
9+
const mkQuestion = () => ({
10+
model: {
11+
ignoreSpacing: true,
12+
},
13+
correctResponse: {
14+
equation: '3x+2',
15+
feedback: {
16+
type: 'default'
17+
}
18+
},
19+
incorrectFeedback: {
20+
type: 'custom',
21+
value: 'foo'
22+
}
23+
});
24+
25+
describe('gather', () => {
26+
27+
beforeEach(async () => {
28+
question = mkQuestion();
29+
session = { value: '3x+2' };
30+
env = { mode: 'gather' };
31+
result = await model(question, session, env);
32+
});
33+
34+
it('returns color_contrast', () => {
35+
expect(result.colorContrast).toEqual('black_on_white');
36+
});
37+
38+
it('returns disabled:false', () => {
39+
expect(result.disabled).toEqual(false);
40+
});
41+
42+
it('returns undefined for correctness ', () => {
43+
expect(result.correctness).toEqual(undefined);
44+
});
45+
46+
it('returns undefined for feedback', () => {
47+
expect(result.feedback).toEqual(undefined);
48+
});
49+
50+
});
51+
52+
describe('view', () => {
53+
54+
beforeEach(async () => {
55+
question = mkQuestion();
56+
session = { value: '3x+2' };
57+
env = { mode: 'view' };
58+
result = await model(question, session, env);
59+
});
60+
61+
it('returns disabled:true', () => {
62+
expect(result.disabled).toEqual(true);
63+
});
64+
65+
it('returns undefined for correctness ', () => {
66+
expect(result.correctness).toEqual(undefined);
67+
});
68+
69+
it('returns default correct for feedback', () => {
70+
expect(result.feedback).toEqual(undefined);
71+
});
72+
73+
});
74+
75+
describe('evaluate - correct', () => {
76+
beforeEach(async () => {
77+
question = mkQuestion();
78+
session = { value: '3x+2' };
79+
env = { mode: 'evaluate' };
80+
result = await model(question, session, env);
81+
});
82+
83+
it('returns disabled:true', () => {
84+
expect(result.disabled).toEqual(true);
85+
});
86+
87+
it('returns correct for correctness ', () => {
88+
expect(result.correctness).toEqual('correct');
89+
});
90+
91+
it('returns default correct for feedback', () => {
92+
expect(result.feedback).toEqual('Correct');
93+
});
94+
});
95+
96+
describe('evaluate - incorrect', () => {
97+
beforeEach(async () => {
98+
question = mkQuestion();
99+
session = { value: '3x+3' };
100+
env = { mode: 'evaluate' };
101+
result = await model(question, session, env);
102+
});
103+
104+
it('returns disabled:true', () => {
105+
expect(result.disabled).toEqual(true);
106+
});
107+
108+
it('returns incorrect for incorrectness ', () => {
109+
expect(result.correctness).toEqual('incorrect');
110+
});
111+
112+
it('returns custom incorrect for feedback', () => {
113+
expect(result.feedback).toEqual('foo');
114+
});
115+
});
116+
});

packages/function-entry/controller/package.json

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@
22
"name": "@pie-element/function-entry-controller",
33
"version": "1.0.0",
44
"description": "",
5-
"main": "src/index.js",
65
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
6+
"test": "./node_modules/.bin/jest"
87
},
98
"author": "",
10-
"license": "ISC"
11-
}
9+
"license": "ISC",
10+
"main": "src/index.js",
11+
"dependencies": {
12+
"debug": "^3.1.0"
13+
},
14+
"devDependencies": {
15+
"jest": "^22.4.0",
16+
"babel-jest": "^22.4.0",
17+
"babel-preset-env": "^1.6.1",
18+
"babel-preset-stage-0": "^6.24.1"
19+
}
20+
}
Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,79 @@
1+
import debug from 'debug';
2+
3+
const log = debug('@pie-element:function-entry:controller');
4+
5+
const process = (v, ignoreWhitespace) => {
6+
let out = v ? v.trim() : '';
7+
out = !ignoreWhitespace ? out : out.replace(/ /g, '');
8+
return out;
9+
};
10+
11+
const isResponseCorrect = (correctResponse, model, value) => {
12+
const processedValue = process(value, model.ignoreSpacing);
13+
14+
return processedValue === correctResponse;
15+
};
16+
117
export function model(question, session, env) {
2-
return Promise.resolve({
3-
disabled: env.mode !== 'gather'
4-
});
5-
}
18+
return new Promise((resolve) => {
19+
20+
const {model, correctResponse} = question;
21+
22+
const defaultFeedback = Object.assign({
23+
correct: 'Correct',
24+
incorrect: 'Incorrect',
25+
empty: 'The answer is empty'
26+
}, question.defaultFeedback);
27+
28+
const getFeedback = (correctness) => {
29+
30+
const fb = (config) => {
31+
config = config || {};
32+
if (config.type === 'custom') {
33+
return config.value;
34+
} else if (config.type === 'default') {
35+
return 'Correct';
36+
}
37+
};
38+
39+
if (env.mode === 'evaluate') {
40+
41+
if (correctness === 'correct') {
42+
return fb(question.correctResponse.feedback, defaultFeedback.correct);
43+
}
44+
45+
if (correctness === 'incorrect') {
46+
return fb(question.incorrectFeedback, defaultFeedback.incorrect);
47+
}
48+
49+
if (correctness === 'empty') {
50+
return defaultFeedback.empty;
51+
}
52+
}
53+
};
54+
55+
const getCorrectness = () => {
56+
if (env.mode === 'evaluate') {
57+
58+
if (!session.value) {
59+
return 'empty';
60+
}
61+
62+
return isResponseCorrect(correctResponse.equation, model, session.value) ? 'correct' : 'incorrect';
63+
}
64+
};
65+
66+
67+
const correctness = getCorrectness();
68+
const base = {
69+
colorContrast: 'black_on_white',
70+
correctness,
71+
feedback: getFeedback(correctness),
72+
disabled: env.mode !== 'gather'
73+
};
74+
75+
const out = Object.assign(base, model);
76+
log('out: ', out);
77+
resolve(out);
78+
});
79+
}

0 commit comments

Comments
 (0)