-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add create and update functionality to the shop client
- Loading branch information
Mina Smart
committed
Feb 25, 2016
1 parent
43b1aa9
commit 9fe1c6c
Showing
5 changed files
with
308 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import BaseModel from 'buy-button-sdk/models/base-model'; | ||
|
||
const CartModel = BaseModel.extend({ | ||
constructor() { | ||
this.super(...arguments); | ||
} | ||
}); | ||
|
||
export default CartModel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import CoreObject from 'buy-button-sdk/metal/core-object'; | ||
import CheckoutModel from 'buy-button-sdk/models/checkout-model'; | ||
|
||
const CheckoutSerializer = CoreObject.extend({ | ||
constructor() { | ||
}, | ||
|
||
rootKeyForType(type) { | ||
return type.slice(0, -1); | ||
}, | ||
|
||
modelForType(/* type */) { | ||
return CheckoutModel; | ||
}, | ||
|
||
deserializeSingle(type, singlePayload, metaAttrs) { | ||
const modelAttrs = singlePayload[this.rootKeyForType(type)]; | ||
const model = this.modelFromAttrs(type, modelAttrs, metaAttrs); | ||
|
||
return model; | ||
}, | ||
|
||
modelFromAttrs(type, attrs, metaAttrs) { | ||
const Model = this.modelForType(type); | ||
|
||
return new Model(attrs, metaAttrs); | ||
}, | ||
|
||
serialize(type, model) { | ||
const root = this.rootKeyForType(type); | ||
const payload = {}; | ||
|
||
payload[root] = model.attrs; | ||
|
||
return payload; | ||
} | ||
}); | ||
|
||
export default CheckoutSerializer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { module, test } from 'qunit'; | ||
import CheckoutSerializer from 'buy-button-sdk/serializers/checkout-serializer'; | ||
import CheckoutModel from 'buy-button-sdk/models/checkout-model'; | ||
|
||
let serializer; | ||
|
||
module('Unit | CheckoutSerializer', { | ||
setup() { | ||
serializer = new CheckoutSerializer(); | ||
}, | ||
teardown() { | ||
serializer = null; | ||
} | ||
}); | ||
|
||
|
||
const checkoutFixture = { | ||
checkout: { | ||
line_items: [] | ||
} | ||
}; | ||
|
||
test('it discovers the root key from the type', function (assert) { | ||
assert.expect(1); | ||
|
||
assert.equal(serializer.rootKeyForType('checkouts'), 'checkout'); | ||
}); | ||
|
||
test('it returns CheckoutModel for checkout type', function (assert) { | ||
assert.expect(1); | ||
|
||
assert.equal(serializer.modelForType('checkouts'), CheckoutModel); | ||
}); | ||
|
||
test('it transforms a single item payload into a checkout object.', function (assert) { | ||
assert.expect(2); | ||
|
||
const model = serializer.deserializeSingle('checkouts', checkoutFixture); | ||
|
||
assert.notOk(Array.isArray(model), 'should not be an array'); | ||
assert.deepEqual(model.attrs, checkoutFixture.checkout); | ||
}); | ||
|
||
test('it attaches a reference of the passed serializer to the model on #deserializeSingle', function (assert) { | ||
assert.expect(1); | ||
|
||
const model = serializer.deserializeSingle('checkouts', checkoutFixture, { serializer }); | ||
|
||
assert.deepEqual(model.serializer, serializer); | ||
}); | ||
|
||
test('it attaches a reference of the passed shopClient to the model on #deserializeSingle', function (assert) { | ||
assert.expect(1); | ||
|
||
const shopClient = 'some-shop-client'; | ||
|
||
const model = serializer.deserializeSingle('checkouts', checkoutFixture, { shopClient }); | ||
|
||
assert.equal(model.shopClient, shopClient); | ||
}); | ||
|
||
test('it transforms a model into a payload on #serialize using the root key', function (assert) { | ||
const updatedModel = new CheckoutModel({ | ||
line_items: [ | ||
{ | ||
variant_id: 123456789, | ||
quantity: 1 | ||
} | ||
] | ||
}); | ||
|
||
const payload = serializer.serialize('checkouts', updatedModel); | ||
|
||
assert.deepEqual(payload, { checkout: updatedModel.attrs }); | ||
}); |