Skip to content

Commit

Permalink
fix(AppCtx): allow falsey values to be set for AppCtx data - testing …
Browse files Browse the repository at this point in the history
…undefined instead of falsey

includes tests covering the cases

affects: @tao.js/core
  • Loading branch information
eudaimos committed Oct 15, 2018
1 parent caa416f commit 75188dc
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 15 deletions.
35 changes: 22 additions & 13 deletions packages/tao/src/AppCtx.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,39 @@ function _cleanDatum(term, action, orient, ...data) {
let checkData = data;
if (checkData.length === 1) {
// MUST check Array first b/c ([] instanceof Object === true)
if (checkData[0] instanceof Array) {
if (Array.isArray(checkData[0])) {
checkData = checkData[0];
} else if (checkData[0] instanceof Object) {
// infer all context data from single object
const obj = checkData[0];
let assigned = false;
if (obj.term || obj.t) {
datum[term] = obj.term || obj.t;
if (typeof obj.term !== 'undefined') {
datum[term] = obj.term;
assigned = true;
} else if (obj[term]) {
} else if (typeof obj.t !== 'undefined') {
datum[term] = obj.t;
assigned = true;
} else if (typeof obj[term] !== 'undefined') {
datum[term] = obj[term];
assigned = true;
}
if (obj.action || obj.a) {
datum[action] = obj.action || obj.a;
if (typeof obj.action !== 'undefined') {
datum[action] = obj.action;
assigned = true;
} else if (typeof obj.a !== 'undefined') {
datum[action] = obj.a;
assigned = true;
} else if (obj[action]) {
} else if (typeof obj[action] !== 'undefined') {
datum[action] = obj[action];
assigned = true;
}
if (obj.orient || obj.o) {
datum[orient] = obj.orient || obj.o;
if (typeof obj.orient !== 'undefined') {
datum[orient] = obj.orient;
assigned = true;
} else if (typeof obj.o !== 'undefined') {
datum[orient] = obj.o;
assigned = true;
} else if (obj[orient]) {
} else if (typeof obj[orient] !== 'undefined') {
datum[orient] = obj[orient];
assigned = true;
}
Expand All @@ -42,13 +51,13 @@ function _cleanDatum(term, action, orient, ...data) {
}
}
// if we are left then assume the first object is term only
if (checkData[0]) {
if (typeof checkData[0] !== 'undefined') {
datum[term] = checkData[0];
}
if (checkData[1]) {
if (typeof checkData[1] !== 'undefined') {
datum[action] = checkData[1];
}
if (checkData[2]) {
if (typeof checkData[2] !== 'undefined') {
datum[orient] = checkData[2];
}
return datum;
Expand Down
84 changes: 82 additions & 2 deletions packages/tao/test/AppCtx.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ describe('AppCtx adds data in order to define concrete Application Contexts duri
TERM,
ACTION,
ORIENT,
null,
undefined,
undefined,
expectedOrientData
);
Expand All @@ -373,7 +373,7 @@ describe('AppCtx adds data in order to define concrete Application Contexts duri
TERM,
ACTION,
ORIENT,
null,
undefined,
undefined,
expectedOrientData
);
Expand Down Expand Up @@ -422,4 +422,84 @@ describe('AppCtx adds data in order to define concrete Application Contexts duri
expect(actual).toEqualOn(expected);
});
});

describe('AppCtx can receive primitive values for data parameters', () => {
it('should allow primitive values for data', () => {
// Assemble
const expectedTermData = 12345;
const expectedActionData = 'thing';
const expectedOrientData = Date();
const expectedTao = { t: TERM, a: ACTION, o: ORIENT };
const expected = {
...expectedTao,
data: {
[TERM]: expectedTermData,
[ACTION]: expectedActionData,
[ORIENT]: expectedOrientData
}
};
// Act
const actualFromArray = new AppCtx(TERM, ACTION, ORIENT, [
expectedTermData,
expectedActionData,
expectedOrientData
]);
const actualFromObject = new AppCtx(TERM, ACTION, ORIENT, {
t: expectedTermData,
action: expectedActionData,
[ORIENT]: expectedOrientData
});
const actualFromArgs = new AppCtx(
TERM,
ACTION,
ORIENT,
expectedTermData,
expectedActionData,
expectedOrientData
);
// Assert
expect(actualFromArray).toEqualOn(expected);
expect(actualFromObject).toEqualOn(expected);
expect(actualFromArgs).toEqualOn(expected);
});

it('should allow falsey primitive values besides undefined for data', () => {
// Assemble
const expectedTermData = 0;
const expectedActionData = '';
const expectedOrientData = null;
const expectedTao = { t: TERM, a: ACTION, o: ORIENT };
const expected = {
...expectedTao,
data: {
[TERM]: expectedTermData,
[ACTION]: expectedActionData,
[ORIENT]: expectedOrientData
}
};
// Act
const actualFromArray = new AppCtx(TERM, ACTION, ORIENT, [
expectedTermData,
expectedActionData,
expectedOrientData
]);
const actualFromObject = new AppCtx(TERM, ACTION, ORIENT, {
t: expectedTermData,
action: expectedActionData,
[ORIENT]: expectedOrientData
});
const actualFromArgs = new AppCtx(
TERM,
ACTION,
ORIENT,
expectedTermData,
expectedActionData,
expectedOrientData
);
// Assert
expect(actualFromArray).toEqualOn(expected);
expect(actualFromObject).toEqualOn(expected);
expect(actualFromArgs).toEqualOn(expected);
});
});
});

0 comments on commit 75188dc

Please sign in to comment.