From 92b5cb90c4c7f2bef29f46be0f87e21e401760ce Mon Sep 17 00:00:00 2001 From: Ignacio Baixas Date: Tue, 23 Sep 2014 09:30:51 -0300 Subject: [PATCH] fix(styles): Fixes AMS style definition, adds a ams-spec. Fixes #151 --- src/styles/ams.js | 4 ++-- test/styles/ams-spec.js | 47 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 test/styles/ams-spec.js diff --git a/src/styles/ams.js b/src/styles/ams.js index 4da2535..b1ad2ac 100644 --- a/src/styles/ams.js +++ b/src/styles/ams.js @@ -11,9 +11,9 @@ angular.module('restmod').factory('AMSApi', ['restmod', 'inflector', function(re jsonLinks: 'links' }, - $methods: { + $extend: { // special snakecase to camelcase renaming - type: { + Model: { decodeName: inflector.camelize, encodeName: function(_v) { return inflector.parameterize(_v, '_'); } } diff --git a/test/styles/ams-spec.js b/test/styles/ams-spec.js new file mode 100644 index 0000000..f81cbb0 --- /dev/null +++ b/test/styles/ams-spec.js @@ -0,0 +1,47 @@ +'use strict'; + +describe('Style: AMS', function() { + + var bike; + + beforeEach(module('restmod')); + + beforeEach(module(function($provide, restmodProvider) { + restmodProvider.rebase('AMSApi'); + $provide.factory('Bike', function(restmod) { + return restmod.model('/api/bikes', { + user: { belongsTo: restmod.model('/api/users') } + }); + }); + })); + + beforeEach(inject(function(Bike) { + bike = Bike.$new(); + })); + + it('should properly rename names on decode/encode', function() { + bike.$decode({ 'rear_wheel': 'crossmax' }); + + expect(bike.rearWheel).toBeDefined(); + expect(bike['rear_wheel']).not.toBeDefined(); + expect(bike.$encode()['rear_wheel']).toBeDefined(); + expect(bike.$encode().rearWheel).not.toBeDefined(); + }); + + it('should use "id" as primary key', function() { + bike.$decode({ id: 1 }); + expect(bike.$pk).toEqual(1); + }); + + it('should extract metadata from "meta" property', function() { + bike.$unwrap({ bike: { id: 1 }, meta: { date: '2014-05-01' } }); + expect(bike.$metadata).toBeDefined(); + expect(bike.$metadata.date).toBeDefined(); + }); + + it('should extract links from "links" property', function() { + bike.$unwrap({ bike: { id: 1, 'user_id': 1 }, links: { users: [ { id: 1, name: 'Pancho' } ] } }); + expect(bike.user).toBeDefined(); + expect(bike.user.name).toEqual('Pancho'); + }); +});