Skip to content

Commit

Permalink
Support objects in multivariant args (#713)
Browse files Browse the repository at this point in the history
* added adjustValue to mutator

* add option to hide icon from TypedInput

* add support for new weighed args format

* fix property error

* fix test

* bump editor version

* code review

* added integration test
  • Loading branch information
nataly87s committed Jan 7, 2018
1 parent 232da4e commit d4c3bad
Show file tree
Hide file tree
Showing 17 changed files with 378 additions and 157 deletions.
22 changes: 22 additions & 0 deletions e2e/integration/spec/tweek-api/value-distribution.test.js
@@ -0,0 +1,22 @@
const { expect } = require('chai');
const { init: initClients } = require('../../utils/clients');

const objectFormatKey = 'integration_tests/value_distribution/object_format';
const arrayFormatKey = 'integration_tests/value_distribution/array_format';

describe('tweek api - value distribution', () => {
let clients;
before(async () => {
clients = await initClients();
});

it('should get correct value - array format', async () => {
const result = await clients.api.get(`/api/v1/keys/${arrayFormatKey}?user=some_user`);
expect(JSON.parse(result.body)).to.equal(15);
});

it('should get correct value - object format', async () => {
const result = await clients.api.get(`/api/v1/keys/${objectFormatKey}?user=some_user`);
expect(JSON.parse(result.body)).to.equal(15);
});
});
19 changes: 14 additions & 5 deletions e2e/ui/spec/keys/multi-variant.js
Expand Up @@ -51,11 +51,20 @@ describe('MultiVariant value type', () => {
});

it('should succeed editing other value types', () => {
const args = {
value_one: 15,
value_two: 25,
value_thee: 60,
};
const args = [
{
value: 'value_one',
weight: 15,
},
{
value: 'value_two',
weight: 25,
},
{
value: 'value_thee',
weight: 60,
},
];
const expectedValue = {
Matcher: {},
Type: 'MultiVariant',
Expand Down
7 changes: 3 additions & 4 deletions e2e/ui/utils/Rule.js
Expand Up @@ -115,17 +115,16 @@ export default class Rule {
}

setValues(args) {
args = Object.entries(args);
const argsCount = browser.elements(`${dataComp('custom-slider')} ${dataComp('legend-item')}`)
.value.length;

for (let i = 0; i < args.length - argsCount; i++) {
browser.click(this._ruleCompSelector('add-variant-button'));
}

args.forEach(([arg, percent], i) => {
browser.setValue(this._sliderComp('legend-value', i + 1), arg);
browser.setValue(this._sliderComp('legend-percent', i + 1), percent);
args.forEach(({ value, weight }, i) => {
browser.setValue(this._sliderComp('legend-value', i + 1), value);
browser.setValue(this._sliderComp('legend-percent', i + 1), weight);
});

return this;
Expand Down
2 changes: 1 addition & 1 deletion services/editor/package.json
@@ -1,6 +1,6 @@
{
"name": "tweek-editor",
"version": "0.3.1",
"version": "0.3.2",
"main": "dist/index.js",
"repository": "Soluto/tweek",
"author": "Soluto",
Expand Down
105 changes: 94 additions & 11 deletions services/editor/spec/unit/utils/mutator-spec.js
Expand Up @@ -16,24 +16,46 @@ describe('mutator tests', () => {
newMutator((target, mutator) => {
target.prop1 = { prop2: 10 };
expect(mutator.in('prop1').getValue().prop2).toEqual(10);
expect(mutator.in('prop1').in('prop2').getValue()).toEqual(10);
expect(
mutator
.in('prop1')
.in('prop2')
.getValue(),
).toEqual(10);
}),
);

it(
'up should allow navigating in object',
newMutator((target, mutator) => {
target.prop1 = { prop2: 10 };
expect(mutator.in('prop1').in('prop2').up().getValue().prop2).toEqual(10);
expect(
mutator
.in('prop1')
.in('prop2')
.up()
.getValue().prop2,
).toEqual(10);
}),
);

it(
'in & up shoul work with arrays',
newMutator((target, mutator) => {
target.propArray = [5, 10];
expect(mutator.in('propArray').in(1).getValue()).toEqual(10);
expect(mutator.in('propArray').in(1).up().getValue()[1]).toEqual(10);
expect(
mutator
.in('propArray')
.in(1)
.getValue(),
).toEqual(10);
expect(
mutator
.in('propArray')
.in(1)
.up()
.getValue()[1],
).toEqual(10);
}),
);
});
Expand All @@ -50,15 +72,22 @@ describe('mutator tests', () => {
it(
'should be able to add a nested field',
newMutator((target, mutator) => {
mutator.insert('someField', {}).in('someField').insert('nested', 10);
mutator
.insert('someField', {})
.in('someField')
.insert('nested', 10);
expect(target.someField.nested).toEqual(10);
}),
);

it(
'should be able to add item to an array at index',
newMutator((target, mutator) => {
mutator.insert('someFieldArray', []).in('someFieldArray').insert(0, 5).insert(1, 10);
mutator
.insert('someFieldArray', [])
.in('someFieldArray')
.insert(0, 5)
.insert(1, 10);
expect(target.someFieldArray[0]).toEqual(5);
expect(target.someFieldArray[1]).toEqual(10);
}),
Expand All @@ -80,10 +109,25 @@ describe('mutator tests', () => {
newMutator((target, mutator) => {
target.someKey = { a: 10 };
expect(Object.keys(target.someKey).length).toEqual(1);
mutator.in('someKey').in('a').delete();
mutator
.in('someKey')
.in('a')
.delete();
expect(Object.keys(target.someKey).length).toEqual(0);
}),
);

it(
'should be able to delete an item from array',
newMutator((target, mutator) => {
target.someKey = [0, 1, 2];
mutator
.in('someKey')
.in(1)
.delete();
expect(target.someKey).toEqual([0, 2]);
}),
);
});

describe('update value', () => {
Expand All @@ -100,7 +144,33 @@ describe('mutator tests', () => {
'should be able to update a value in array',
newMutator((target, mutator) => {
target.someArray = [10, 20];
mutator.in('someArray').in(1).updateValue(30);
mutator
.in('someArray')
.in(1)
.updateValue(30);
expect(target.someArray[1]).toEqual(30);
}),
);
});

describe('adjust value', () => {
it(
'should be able to adjust a field value',
newMutator((target, mutator) => {
target.someKey = 5;
mutator.in('someKey').adjustValue(x => x + 5);
expect(target.someKey).toEqual(10);
}),
);

it(
'should be able to adjust a value in array',
newMutator((target, mutator) => {
target.someArray = [10, 20];
mutator
.in('someArray')
.in(1)
.adjustValue(x => x + 10);
expect(target.someArray[1]).toEqual(30);
}),
);
Expand All @@ -120,15 +190,21 @@ describe('mutator tests', () => {
'should update the mutator path after key is updated',
newMutator((target, mutator) => {
target.someKey = 5;
mutator.in('someKey').updateKey('newKey').updateValue(10);
mutator
.in('someKey')
.updateKey('newKey')
.updateValue(10);
expect(target.newKey).toEqual(10);
}),
);
it(
'should preserve order in object',
newMutator((target, mutator) => {
target.largeObj = { a: 5, b: 10, c: 30, d: 40 };
mutator.in('largeObj').in('c').updateKey('e');
mutator
.in('largeObj')
.in('c')
.updateKey('e');
expect(Object.keys(target.largeObj)).toEqual(['a', 'b', 'e', 'd']);
}),
);
Expand Down Expand Up @@ -203,7 +279,14 @@ describe('mutator tests', () => {
mutator.in('a').updateValue(1);
mutator.in('b').updateValue(1);
expect(mock.mock.calls.length).toEqual(2);
mutator.apply(m => m.in('a').updateValue(2).up().in('b').updateValue(2));
mutator.apply(m =>
m
.in('a')
.updateValue(2)
.up()
.in('b')
.updateValue(2),
);
expect(mock.mock.calls.length).toEqual(3);
const applyMutation = mock.mock.calls[2][0];
expect(applyMutation.a).toEqual(2);
Expand Down

0 comments on commit d4c3bad

Please sign in to comment.