Skip to content

Commit

Permalink
Merge pull request #622 from mesosphere/refactor/3011-getter-appversi…
Browse files Browse the repository at this point in the history
…on-store

Refactor - 3011 Getter for  AppVersionStore
  • Loading branch information
Pierluigi Cau committed Feb 10, 2016
2 parents cd0d847 + 5089831 commit c29d983
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 28 deletions.
49 changes: 31 additions & 18 deletions src/js/stores/AppVersionsStore.js
@@ -1,50 +1,63 @@
import {EventEmitter} from "events";
import lazy from "lazy.js";

import AppDispatcher from "../AppDispatcher";
import appScheme from "../stores/schemes/appScheme";
import AppVersionsEvents from "../events/AppVersionsEvents";

import Util from "../helpers/Util";

const storeData = {
appVersions: {},
availableAppVersions: [],
currentAppId: null
};

function processAppVersion(appVersion) {
return lazy(appScheme).extend(appVersion).value();
return Util.extendObject(appScheme, appVersion);
}

var AppVersionsStore = lazy(EventEmitter.prototype).extend({
// appId where the app versions belong to
currentAppId: null,
// List of the available version timestamps
availableAppVersions: [],
var AppVersionsStore = Util.extendObject(EventEmitter.prototype, {
// Already requested versions with version timestamp as key
appVersions: {},
get appVersions() {
return Util.deepCopy(storeData.appVersions);
},
// List of the available version timestamps
get availableAppVersions() {
return Util.deepCopy(storeData.availableAppVersions);
},
// appId where the app versions belong to
get currentAppId() {
return storeData.currentAppId;
},

resetOnAppChange: function (appId) {
if (appId !== this.currentAppId) {
this.availableAppVersions = [];
this.appVersions = {};
this.currentAppId = appId;
if (appId !== storeData.currentAppId) {
storeData.availableAppVersions = [];
storeData.appVersions = {};
storeData.currentAppId = appId;
}
},

getAppVersions: function (appId) {
if (appId === this.currentAppId) {
if (appId === storeData.currentAppId) {
return this.availableAppVersions;
}
return [];
},

getAppVersion: function (appId, appVersionTimestamp) {
if (appId === this.currentAppId) {
return this.appVersions[appVersionTimestamp] || {};
if (appId === storeData.currentAppId) {
return Util.deepCopy(this.appVersions[appVersionTimestamp]) || {};
}
return {};
}
}).value();
});

AppDispatcher.register(function (action) {
switch (action.actionType) {
case AppVersionsEvents.REQUEST_VERSION_TIMESTAMPS:
AppVersionsStore.resetOnAppChange(action.appId);
AppVersionsStore.availableAppVersions = action.data.body.versions;
storeData.availableAppVersions = action.data.body.versions;
AppVersionsStore.emit(AppVersionsEvents.CHANGE, action.appId);
break;
case AppVersionsEvents.REQUEST_VERSION_TIMESTAMPS_ERROR:
Expand All @@ -56,7 +69,7 @@ AppDispatcher.register(function (action) {
break;
case AppVersionsEvents.REQUEST_ONE:
AppVersionsStore.resetOnAppChange(action.appId);
AppVersionsStore.appVersions[action.versionTimestamp] =
storeData.appVersions[action.versionTimestamp] =
processAppVersion(action.data.body);
AppVersionsStore.emit(AppVersionsEvents.CHANGE, action.versionTimestamp);
break;
Expand Down
33 changes: 23 additions & 10 deletions src/test/units/AppVersionListComponent.test.js
Expand Up @@ -2,23 +2,36 @@ import {expect} from "chai";
import {mount} from "enzyme";

import React from "../../../node_modules/react/addons";
import AppVersionsStore from "../../js/stores/AppVersionsStore";
import AppVersionListComponent
from "../../js/components/AppVersionListComponent.jsx";
import AppVersionListItemComponent
from "../../js/components/AppVersionListItemComponent.jsx";

describe("AppVersionListComponent", function () {
import AppDispatcher from "../../js/AppDispatcher";
import AppVersionsEvents from "../../js/events/AppVersionsEvents";
import AppVersionsStore from "../../js/stores/AppVersionsStore";

before(function () {
AppVersionsStore.currentAppId = "/app-test";
AppVersionsStore.availableAppVersions = [
"2015-06-29T13:54:01.577Z",
"2015-06-29T13:02:29.615Z",
"2015-06-29T13:02:19.363Z"
];
describe("AppVersionListComponent", function () {

this.component = mount(<AppVersionListComponent appId={"/app-test"} />);
before(function (done) {
var appVersions = {
versions: [
"2015-06-29T13:54:01.577Z",
"2015-06-29T13:02:29.615Z",
"2015-06-29T13:02:19.363Z"
]
};

AppVersionsStore.once(AppVersionsEvents.CHANGE, () => {
this.component = mount(<AppVersionListComponent appId="/app-test" />);
done();
});

AppDispatcher.dispatch({
actionType: AppVersionsEvents.REQUEST_VERSION_TIMESTAMPS,
data: {body: appVersions},
appId: "/app-test"
});
});

after(function () {
Expand Down

0 comments on commit c29d983

Please sign in to comment.