Skip to content
This repository has been archived by the owner on Jun 3, 2022. It is now read-only.

Commit

Permalink
Adds return value to remixer.cloneAndUpdateVariable(). Adds tests. (#85)
Browse files Browse the repository at this point in the history
* Adds return value to remixer.cloneAndUpdateVariable(). Adds tests.

* Refactors return.
  • Loading branch information
chriscox authored Mar 27, 2017
1 parent 345ffda commit 673bc71
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/core/Remixer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,16 +305,20 @@ class Remixer {
* @static
* @param {Variable} variable The variable to clone and update.
* @param {any} selectedValue The new selected value.
* @return {Variable} The cloned variable with updated selected value.
*/
static cloneAndUpdateVariable(variable: Variable, selectedValue: any): void {
static cloneAndUpdateVariable(variable: Variable, selectedValue: any): Variable {
// First make sure selected value is in proper format.
selectedValue = variable.formatValue(selectedValue);

if (variable.selectedValue !== selectedValue) {
let clonedVariable = variable.clone();
this.attachedInstance._variables[variable.key] = clonedVariable;
this.updateVariable(clonedVariable, selectedValue);
if (variable.selectedValue === selectedValue) {
return variable;
}

let clonedVariable = variable.clone();
this.attachedInstance._variables[variable.key] = clonedVariable;
this.updateVariable(clonedVariable, selectedValue);
return clonedVariable;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/core/__tests__/ColorVariable_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as sinon from "sinon";
import * as sinonChai from "sinon-chai";

import { remixer } from "../Remixer";
import { ColorUtils } from "../../lib/ColorUtils";
import { ColorVariable } from "../variables/ColorVariable";
import { ConstraintType, ControlType, DataType } from "../../lib/Constants";
import { Variable } from "../variables/Variable";
Expand Down Expand Up @@ -81,4 +82,13 @@ describe("ColorVariable", () => {
let clone = variable.clone();
expect(JSON.stringify(clone)).to.equal(JSON.stringify(variable));
});

it("should return string color value after format", () => {
let color = "rgba(1, 1, 1, 0.8)";
let rgbaColor = ColorUtils.toRgba(color);
let formattedColor = variable.formatValue(rgbaColor);

expect(color).to.equal(formattedColor);
expect(color).to.not.equal(rgbaColor);
});
});
16 changes: 14 additions & 2 deletions src/core/__tests__/Remixer_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ describe("Remixer", () => {
remixer.addBooleanVariable("key1", true);
remixer.addStringVariable("key2", "testString");
remixer.addNumberVariable("key3", 40);
remixer.addColorVariable("key4", "#4285F4", ["#4285F4", "#0F9D58"]);
remixer.addRangeVariable("key5", 24, 20, 40, 2);
});

it("should create an iframe after start", () => {
Expand All @@ -25,12 +27,12 @@ describe("Remixer", () => {

it("have the correct number of variables in array", () => {
let variablesArray = remixer.attachedInstance.variablesArray;
expect(variablesArray).to.have.length(3);
expect(variablesArray).to.have.length(5);
});

it("should retrieve variables from map", () => {
let variablesMap = remixer.attachedInstance.variables;
expect(variablesMap).to.have.all.keys("key1", "key2", "key3");
expect(variablesMap).to.have.all.keys("key1", "key2", "key3", "key4", "key5");
});

it("should retrieve correct variable from map by key", () => {
Expand All @@ -45,4 +47,14 @@ describe("Remixer", () => {
remixer.updateVariable(numberVariable, 50);
expect(numberVariable.selectedValue).to.equal(50);
});

it("should clone and update selected value of variable", () => {
let rangeVariable = remixer.getVariable("key5");
let clone = remixer.cloneAndUpdateVariable(rangeVariable, 30);

expect(rangeVariable.dataType).to.equal(DataType.NUMBER);
expect(rangeVariable.selectedValue).to.equal(24);
expect(clone.dataType).to.equal(DataType.NUMBER);
expect(clone.selectedValue).to.equal(30);
});
});

0 comments on commit 673bc71

Please sign in to comment.