diff --git a/package.json b/package.json index b16355f..c1986ed 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "dependencies": { "can-component": "^3.0.0-pre.8", "can-connect": "^0.6.0-pre.4", - "can-define": "^0.7.4", + "can-define": "^0.7.24", "can-route": "^3.0.0-pre.5", "can-route-pushstate": "^3.0.0-pre.3", "can-stache": "^3.0.0-pre.6", diff --git a/src/models/account-test.js b/src/models/account-test.js new file mode 100644 index 0000000..cc3e647 --- /dev/null +++ b/src/models/account-test.js @@ -0,0 +1,49 @@ +import 'steal-mocha'; +import chai from 'chai'; +import {Account} from './account'; + +const assert = chai.assert; + +describe('models/account', function () { + it('gets all account information', function (done) { + Account.getList().then(function (accounts) { + assert.equal(accounts.length, 1); + assert.equal(accounts[0].email, 'mark@bitovi.com'); + done(); + }); + }); + it('gets specific account information', function (done) { + Account.get({id: 614}).then(function (account) { + assert.equal(account.email, 'mark@bitovi.com'); + done(); + }); + }); + it('creates a new account', function (done) { + const newAccount = new Account({ + name: 'name', + email: 'example@example.com', + organizations: [{ + id: 129, + name: 'Wow Bao' + }] + }); + assert.equal(newAccount.email, 'example@example.com'); + done(); + }); + it('checks to see if an account has multiple organizations', function (done) { + const newAccount = new Account({ + id: 123, + name: 'name', + email: 'example@example.com', + organizations: [{ + id: 129, + name: 'Wow Bao' + }] + }); + assert.equal(newAccount.hasMultipleOrganizations(), false); + Account.get({id: 614}).then(function (account) { + assert.equal(account.hasMultipleOrganizations(), true); + done(); + }); + }); +}); diff --git a/src/models/account.js b/src/models/account.js new file mode 100644 index 0000000..f75cd96 --- /dev/null +++ b/src/models/account.js @@ -0,0 +1,42 @@ +import DefineMap from 'can-define/map/'; +import DefineList from 'can-define/list/'; +import superMap from 'can-connect/can/super-map/'; +import tag from 'can-connect/can/tag/'; + +// import Organization from './organization.js'; + +export const Account = DefineMap.extend({ + // properties + id: '*', + organizations: Array, + password: { + value: '' + }, + current_password: { + value: '' + }, + email: 'string', + name: 'string', + + // methods + hasMultipleOrganizations() { + const organizations = this.organizations; + return organizations && organizations.length > 1; + } +}); + +Account.List = DefineList.extend({ + '*': Account +}); + +export const accountConnection = superMap({ + url: '/api/v3/current/account', + idProp: 'id', + Map: Account, + List: Account.List, + name: 'account' +}); + +tag('account-model', accountConnection); + +export default Account; diff --git a/src/models/fixtures/account.js b/src/models/fixtures/account.js new file mode 100644 index 0000000..c7916f2 --- /dev/null +++ b/src/models/fixtures/account.js @@ -0,0 +1,14 @@ +import fixture from 'can-fixture'; +import account from './data/current-account.json'; + +const store = fixture.store([account]); + +fixture({ + 'GET /api/v3/current/account': store.findAll, + 'GET /api/v3/current/account/{id}': store.findOne, + 'POST /api/v3/current/account': store.create, + 'PUT /api/v3/current/account/{id}': store.update, + 'DELETE /api/v3/current/account/{id}': store.destroy +}); + +export default store; diff --git a/src/models/fixtures/fixtures.js b/src/models/fixtures/fixtures.js index 4be8c53..9d23741 100644 --- a/src/models/fixtures/fixtures.js +++ b/src/models/fixtures/fixtures.js @@ -1 +1,3 @@ // Main file that loads all model fixtures + +import 'bithub-admin/models/fixtures/account'; \ No newline at end of file diff --git a/src/models/test.js b/src/models/test.js index 8b78254..014ab65 100644 --- a/src/models/test.js +++ b/src/models/test.js @@ -1 +1,3 @@ import './fixtures/'; + +import 'bithub-admin/models/account-test';