Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add serialized information to queryParamsState #47

Merged
merged 1 commit into from
Dec 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion addon/-private/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import Ember from 'ember';

const {
assert,
isPresent
isPresent,
canInvoke
} = Ember;

/**
Expand All @@ -16,8 +17,12 @@ const {
function queryParamsState(queryParamsArray, controller) {
return queryParamsArray.reduce((state, qp) => {
let value = qp.value(controller);
let serializedValue = canInvoke(qp, 'serialize') ? qp.serialize(value) : value;
let as = qp.as || qp.key;
state[qp.key] = {
value,
serializedValue,
as,
defaultValue: qp.defaultValue,
changed: JSON.stringify(value) !== JSON.stringify(qp.defaultValue)
};
Expand Down
43 changes: 36 additions & 7 deletions tests/unit/query-params-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,24 @@ const queryParams = new QueryParams({
search: {
defaultValue: '',
refresh: true
},
colors: {
defaultValue: [],
refresh: true,
serialize(value) {
return value.toString();
},
deserialize(value = '') {
return value.split(',');
}
}
});

const defaultValues = {
direction: 'asc',
page: 1,
search: ''
search: '',
colors: []
}

const Controller = Ember.Object.extend(queryParams.Mixin);
Expand Down Expand Up @@ -139,40 +150,58 @@ test('CP - allQueryParams', function(assert) {
assert.deepEqual(controller.get('allQueryParams'), {
direction: 'asc',
page: 1,
search: ''
search: '',
colors: []
});

controller.set('page', 2);

assert.deepEqual(controller.get('allQueryParams'), {
direction: 'asc',
page: 2,
search: ''
search: '',
colors: []
});
});

test('CP - queryParamsState', function(assert) {
assert.expect(3);
assert.expect(4);

assert.deepEqual(controller.get('queryParamsState.page'), {
value: 1,
defaultValue: 1,
changed: false
changed: false,
as: 'page',
serializedValue: 1
});

controller.set('page', 2);

assert.deepEqual(controller.get('queryParamsState.page'), {
value: 2,
defaultValue: 1,
changed: true
changed: true,
as: 'page',
serializedValue: 2
});

controller.setDefaultQueryParamValue('page', 2);

assert.deepEqual(controller.get('queryParamsState.page'), {
value: 2,
defaultValue: 2,
changed: false
changed: false,
as: 'page',
serializedValue: 2
});

controller.set('colors', ['red', 'blue']);

assert.deepEqual(controller.get('queryParamsState.colors'), {
value: ['red', 'blue'],
serializedValue: 'red,blue',
defaultValue: [],
changed: true,
as: 'colors'
});
});