Skip to content
Browse files

Refactoring testu

  • Loading branch information...
1 parent f2e6cfa commit 44c297cde1fe17ef9034feff2fdefe618ea2e6a6 @JakubMrozek committed
View
78 public/js/BasketStorage.js → public/js/Basket.js
@@ -4,7 +4,7 @@
*
*/
-function BasketStorage(window, listener) {
+function Basket(window, listener) {
this._storage = window.localStorage;
this._listener = listener;
this._setEventStorage(window);
@@ -12,18 +12,23 @@ function BasketStorage(window, listener) {
//data o produktech
-BasketStorage.NS_PRODUCTS = 'Products';
+Basket.NS_PRODUCTS = 'products';
//data o zakaznikovi
-BasketStorage.NS_CUSTOMER = 'Customer';
+Basket.NS_CUSTOMER = 'customer';
//data o doprave
-BasketStorage.NS_TRANSPORT = 'Transport';
+Basket.NS_TRANSPORT = 'transport';
-BasketStorage.prototype.notify = function() {
+/**
+ * Upozorni dalsi okna na zmenu modelu v jinem okne.
+ *
+ */
+
+Basket.prototype.notify = function() {
this._listener.call(this);
};
@@ -34,7 +39,7 @@ BasketStorage.prototype.notify = function() {
* @param {Object} product
*/
-BasketStorage.prototype.add = function(product) {
+Basket.prototype.add = function(product) {
var products = this.getAll();
products.push(product);
this._saveProducts(products);
@@ -49,7 +54,7 @@ BasketStorage.prototype.add = function(product) {
* @return {Boolean}
*/
-BasketStorage.prototype.exist = function(id, variant) {
+Basket.prototype.exist = function(id, variant) {
return Boolean(this.get(id, variant));
};
@@ -62,7 +67,7 @@ BasketStorage.prototype.exist = function(id, variant) {
* @return {Object}
*/
-BasketStorage.prototype.get = function(id, variant) {
+Basket.prototype.get = function(id, variant) {
var products = this.getAll();
for (var i = 0; i < products.length; ++i) {
if (this._equals(products[i], id, variant)) {
@@ -78,8 +83,8 @@ BasketStorage.prototype.get = function(id, variant) {
* @return {Array}
*/
-BasketStorage.prototype.getAll = function() {
- var products = this._storage.getItem(BasketStorage.NS_PRODUCTS);
+Basket.prototype.getAll = function() {
+ var products = this._storage.getItem(Basket.NS_PRODUCTS);
//v ulozisti uz drive neco bylo.
if (typeof products === 'string') {
@@ -101,7 +106,7 @@ BasketStorage.prototype.getAll = function() {
* @return {Boolean}
*/
-BasketStorage.prototype.hasProducts = function() {
+Basket.prototype.hasProducts = function() {
return Object.keys(this.getAll()).length > 0;
};
@@ -112,7 +117,7 @@ BasketStorage.prototype.hasProducts = function() {
* @return {Boolean}
*/
-BasketStorage.prototype.hasCustomer = function() {
+Basket.prototype.hasCustomer = function() {
var customer = this.getCustomer() || {};
return Object.keys(customer).length > 0;
};
@@ -127,7 +132,7 @@ BasketStorage.prototype.hasCustomer = function() {
* @return {Boolean}
*/
-BasketStorage.prototype.updateQuantity = function(quantity, id, variant) {
+Basket.prototype.updateQuantity = function(quantity, id, variant) {
var products = this.getAll();
for (var i = 0; i < products.length; ++i) {
if (this._equals(products[i], id, variant)) {
@@ -146,7 +151,7 @@ BasketStorage.prototype.updateQuantity = function(quantity, id, variant) {
* @param {String} variant Varianta produktu.
*/
-BasketStorage.prototype.remove = function(id, variant) {
+Basket.prototype.remove = function(id, variant) {
var oldProducts = this.getAll();
var newProducts = [];
for (var i = 0; i < oldProducts.length; ++i) {
@@ -163,10 +168,10 @@ BasketStorage.prototype.remove = function(id, variant) {
*
*/
-BasketStorage.prototype.clear = function() {
- this._storage.removeItem(BasketStorage.NS_PRODUCTS);
- this._storage.removeItem(BasketStorage.NS_CUSTOMER);
- this._storage.removeItem(BasketStorage.NS_TRANSPORT);
+Basket.prototype.clear = function() {
+ this._storage.removeItem(Basket.NS_PRODUCTS);
+ this._storage.removeItem(Basket.NS_CUSTOMER);
+ this._storage.removeItem(Basket.NS_TRANSPORT);
}
@@ -176,8 +181,8 @@ BasketStorage.prototype.clear = function() {
* @return {Object}
*/
-BasketStorage.prototype.getCustomer = function() {
- return JSON.parse(this._storage.getItem(BasketStorage.NS_CUSTOMER));
+Basket.prototype.getCustomer = function() {
+ return JSON.parse(this._storage.getItem(Basket.NS_CUSTOMER));
};
@@ -187,8 +192,8 @@ BasketStorage.prototype.getCustomer = function() {
* @param {Object} data
*/
-BasketStorage.prototype.updateCustomer = function(data) {
- this._storage.setItem(BasketStorage.NS_CUSTOMER, JSON.stringify(data));
+Basket.prototype.updateCustomer = function(data) {
+ this._storage.setItem(Basket.NS_CUSTOMER, JSON.stringify(data));
};
@@ -198,8 +203,8 @@ BasketStorage.prototype.updateCustomer = function(data) {
* @return {Object}
*/
-BasketStorage.prototype.getTransport = function() {
- return JSON.parse(this._storage.getItem(BasketStorage.NS_TRANSPORT));
+Basket.prototype.getTransport = function() {
+ return JSON.parse(this._storage.getItem(Basket.NS_TRANSPORT));
};
@@ -209,8 +214,8 @@ BasketStorage.prototype.getTransport = function() {
* @param {Object} data
*/
-BasketStorage.prototype.updateTransport = function(data) {
- this._storage.setItem(BasketStorage.NS_TRANSPORT, JSON.stringify(data));
+Basket.prototype.updateTransport = function(data) {
+ this._storage.setItem(Basket.NS_TRANSPORT, JSON.stringify(data));
};
@@ -220,7 +225,7 @@ BasketStorage.prototype.updateTransport = function(data) {
* @return {Number}
*/
-BasketStorage.prototype.priceProducts = function() {
+Basket.prototype.priceProducts = function() {
var products = this.getAll();
var price = 0;
for (var id in products) {
@@ -236,7 +241,7 @@ BasketStorage.prototype.priceProducts = function() {
* @return {Number}
*/
-BasketStorage.prototype.priceTotal = function() {
+Basket.prototype.priceTotal = function() {
return this.priceProducts() + this.getTransport().price;
};
@@ -248,8 +253,8 @@ BasketStorage.prototype.priceTotal = function() {
* @param {Object} products
*/
-BasketStorage.prototype._saveProducts = function(products) {
- this._storage.setItem(BasketStorage.NS_PRODUCTS, JSON.stringify(products));
+Basket.prototype._saveProducts = function(products) {
+ this._storage.setItem(Basket.NS_PRODUCTS, JSON.stringify(products));
};
@@ -259,12 +264,21 @@ BasketStorage.prototype._saveProducts = function(products) {
* @param {String} variant Varianta produktu.
*/
-BasketStorage.prototype._equals = function(product, id, variant) {
+Basket.prototype._equals = function(product, id, variant) {
return product.id === id && product.variant === variant;
};
-BasketStorage.prototype._setEventStorage = function(window) {
+/**
+ * Nastavi udalost storage pro zmenu obsahu kosiku.
+ *
+ * Pri zmene jsou upozorneny vsechny ostatni okna prohlizece mimo to,
+ * ve kterém změna proběhla.
+ *
+ * @param {window} window
+ */
+
+Basket.prototype._setEventStorage = function(window) {
var basket = this;
window.addEventListener('storage', function(){
basket.notify.call(basket)
View
45 public/js/controllers.js
@@ -5,9 +5,8 @@
*
*/
-function AppCtrl($scope, storage) {
- $scope.storage = storage;
- console.log('zavolano');
+function AppCtrl($scope, basket) {
+ $scope.basket = basket;
}
@@ -90,10 +89,10 @@ function CategoryCtrl($scope, $routeParams, api) {
*
*/
-function ProductCtrl($scope, $routeParams, $location, api, storage) {
+function ProductCtrl($scope, $routeParams, $location, api, basket) {
$scope.addToBasket = function(variant){
- if (!storage.exist($scope.product.id, variant.name)) {
- storage.add({
+ if (!basket.exist($scope.product.id, variant.name)) {
+ basket.add({
id: $scope.product.id,
name: $scope.product.name,
url: $scope.product.url,
@@ -113,9 +112,9 @@ function ProductCtrl($scope, $routeParams, $location, api, storage) {
*
*/
-function BasketCtrl($scope, $location, storage) {
+function BasketCtrl($scope, $location, basket) {
$scope.step = 'basket';
- $scope.products = storage.getAll();
+ $scope.products = basket.getAll();
$scope.next = function() {
$location.path('/zakaznicke-udaje');
};
@@ -128,22 +127,22 @@ function BasketCtrl($scope, $location, storage) {
* radio input v ng-repeat: https://github.com/angular/angular.js/issues/1100
*/
-function CustomerCtrl($scope, $location, storage, transport) {
- if (!storage.hasProducts()) {
+function CustomerCtrl($scope, $location, basket, transport) {
+ if (!basket.hasProducts()) {
$location.path('/kosik');
return;
}
$scope.step = 'customer';
- $scope.storage = storage;
+ $scope.basket = basket;
- $scope.customer = storage.getCustomer();
- $scope.transport = storage.getTransport() || {code: 'personal'};
+ $scope.customer = basket.getCustomer();
+ $scope.transport = basket.getTransport() || {code: 'personal'};
$scope.transportMethods = transport.methods();
$scope.next = function() {
- storage.updateCustomer($scope.customer);
- storage.updateTransport(transport.get($scope.transport.code));
+ basket.updateCustomer($scope.customer);
+ basket.updateTransport(transport.get($scope.transport.code));
$location.path('/potvrzeni');
}
}
@@ -154,19 +153,19 @@ function CustomerCtrl($scope, $location, storage, transport) {
*
*/
-function SummaryCtrl($scope, $location, api, storage) {
- if (!storage.hasCustomer() || !storage.hasProducts()) {
+function SummaryCtrl($scope, $location, api, basket) {
+ if (!basket.hasCustomer() || !basket.hasProducts()) {
$location.path('/kosik');
return;
}
$scope.step = 'summary';
- $scope.storage = storage;
+ $scope.basket = basket;
- $scope.products = storage.getAll();
- $scope.customer = storage.getCustomer();
- $scope.transport = storage.getTransport();
- $scope.price = storage.priceTotal();
+ $scope.products = basket.getAll();
+ $scope.customer = basket.getCustomer();
+ $scope.transport = basket.getTransport();
+ $scope.priceTotal = basket.priceTotal();
$scope.next = function() {
var data = {
@@ -177,7 +176,7 @@ function SummaryCtrl($scope, $location, api, storage) {
api.order.create(data, function(info){
$scope.number = info.number;
- storage.clear();
+ basket.clear();
});
}
}
View
6 public/js/services.js
@@ -36,11 +36,11 @@ angular.module('zdrojakServices', ['ngResource'])
var transport = new Transport();
return transport;
-}).factory('storage', function($window, $rootScope){
- var storage = new BasketStorage($window, function(){
+}).factory('basket', function($window, $rootScope){
+ var basket = new Basket($window, function(){
$rootScope.$apply();
});
- return storage;
+ return basket;
});
View
12 public/partials/_basket-header.html
@@ -1,16 +1,16 @@
<div class="page-header" ng-switch on="step">
<h1 ng-switch-when="basket">
1. Košík
- <small ng-hide="storage.hasProducts()">2. Zákaznické údaje</small>
- <small ng-show="storage.hasProducts()"><a ng-href="/zakaznicke-udaje">2. Zákaznické údaje</a></small>
- <small ng-hide="storage.hasCustomer()">3. Přehled</small>
- <small ng-show="storage.hasCustomer()"><a ng-href="/potvrzeni">3. Přehled</a></small>
+ <small ng-hide="basket.hasProducts()">2. Zákaznické údaje</small>
+ <small ng-show="basket.hasProducts()"><a ng-href="/zakaznicke-udaje">2. Zákaznické údaje</a></small>
+ <small ng-hide="basket.hasCustomer()">3. Přehled</small>
+ <small ng-show="basket.hasCustomer()"><a ng-href="/potvrzeni">3. Přehled</a></small>
</h1>
<h1 ng-switch-when="customer">
<small><a ng-href="/kosik">1. Košík</a></small>
2. Zákaznické údaje
- <small ng-hide="storage.hasCustomer()">3. Přehled</small>
- <small ng-show="storage.hasCustomer()"><a ng-href="/potvrzeni">3. Přehled</a></small>
+ <small ng-hide="basket.hasCustomer()">3. Přehled</small>
+ <small ng-show="basket.hasCustomer()"><a ng-href="/potvrzeni">3. Přehled</a></small>
</h1>
<h1 ng-switch-when="summary">
<small><a ng-href="/kosik">1. Košík</a> <a ng-href="/zakaznicke-udaje">2. Zákaznické údaje</a></small> 3. Přehled
View
10 public/partials/basket.html
@@ -1,10 +1,10 @@
<div ng-include src="'/partials/_basket-header.html'"></div>
-<p ng-hide="storage.hasProducts()" class="alert alert-info">
+<p ng-hide="basket.hasProducts()" class="alert alert-info">
V košíku není žádné zboží.
</p>
-<form ng-show="storage.hasProducts()">
+<form ng-show="basket.hasProducts()">
<table class="table table-striped">
<thead>
<tr>
@@ -21,14 +21,14 @@
{{product.variant}}
</td>
<td>{{product.price | currency}}</td>
- <td><input min="1" type="number" ng-change="storage.updateQuantity(product.quantity, product.id, product.variant)" ng-model="product.quantity" class="span1"></td>
- <td><button ng-click="storage.remove(product.id, product.variant)" class="btn" type="button"><i class="icon-remove"></i> Odstranit</button></td>
+ <td><input min="1" type="number" ng-change="basket.updateQuantity(product.quantity, product.id, product.variant)" ng-model="product.quantity" class="span1"></td>
+ <td><button ng-click="basket.remove(product.id, product.variant)" class="btn" type="button"><i class="icon-remove"></i> Odstranit</button></td>
</tr>
<tr>
<th>Celkem</th>
<td></td>
<td></td>
- <th>{{storage.priceProducts() | currency}}</th>
+ <th>{{basket.priceProducts() | currency}}</th>
</tr>
</tbody>
</table>
View
2 public/partials/summary.html
@@ -35,7 +35,7 @@
<th>Celkem</th>
<td></td>
<td></td>
- <th>{{price | currency}}</th>
+ <th>{{priceTotal | currency}}</th>
</tr>
</tbody>
</table>
View
100 test/frontend/unit/Basket.test.js
@@ -0,0 +1,100 @@
+describe('Basket', function(){
+
+ var basket;
+
+ beforeEach(function(){
+ basket = new Basket(window);
+ basket.clear();
+ });
+
+ it('prida novy produkt s variantou do uloziste', function(){
+ basket.add({id: 12345, variant: 'cerny'});
+ expect(basket.exist(12345, 'cerny')).toBeTruthy();
+ expect(basket.get(12345, 'cerny')).toEqual({id: 12345, variant: 'cerny'});
+ });
+
+
+ it('prida novy produkt bez varianty do uloziste', function(){
+ basket.add({id: 12345});
+ expect(basket.exist(12345)).toBeTruthy();
+ expect(basket.get(12345)).toEqual({id: 12345});
+ });
+
+ it('zmeni mnozstvi produktu s variantou v kosiku', function(){
+ basket.add({id: 12345, variant: 'cerny'});
+ basket.updateQuantity(10, 12345, 'cerny');
+ expect(basket.get(12345, 'cerny').quantity).toEqual(10);
+ });
+
+ it('zmeni mnozstvi produktu bez varianty v kosiku', function(){
+ basket.add({id: 12345});
+ basket.updateQuantity(10, 12345);
+ expect(basket.get(12345).quantity).toEqual(10);
+ });
+
+ it('odstrani produkt bez varianty z kosiku', function(){
+ basket.add(12345, {});
+ basket.remove(12345);
+ expect(basket.get(12345)).toBeUndefined();
+ });
+
+ it('odstrani produkt s variantou z kosiku', function(){
+ basket.add({id: 12345, variant: 'cerny'});
+ basket.add({id: 12345, variant: 'bily'});
+ basket.remove(12345, 'cerny');
+ expect(basket.get(12345, 'cerny')).toBeUndefined();
+ expect(basket.get(12345, 'bily')).toBeDefined();
+ });
+
+ it('vrati vsechny produkty v kosiku', function(){
+ basket.add({id: 12456, variant: 'cerny'});
+ basket.add({id: 12457, variant: 'bily'});
+ basket.add({id: 12459, variant: 'modry'});
+ expect(basket.getAll()).toEqual([
+ {id: 12456, variant: 'cerny'},
+ {id: 12457, variant: 'bily'},
+ {id: 12459, variant: 'modry'}
+ ]);
+ });
+
+ it('edituje data uzivatele', function(){
+ basket.updateCustomer({name: 'Jakub', surname: 'Mrozek'});
+ expect(basket.getCustomer()).toEqual({name: 'Jakub', surname: 'Mrozek'});
+ });
+
+ it('edituje informace o doprave', function(){
+ basket.updateTransport({name: 'Doprava ABC'});
+ expect(basket.getTransport()).toEqual({name: 'Doprava ABC'});
+ });
+
+ it('vymaze obsah kosiku', function(){
+ basket.add({id: 12456, variant: 'cerny'});
+ basket.updateCustomer({name: 'Jakub', surname: 'Mrozek'});
+ basket.updateTransport({name: 'Doprava ABC'});
+ basket.clear();
+ expect(basket.getAll()).toEqual([]);
+ expect(basket.getCustomer()).toBeNull();
+ expect(basket.getTransport()).toBeNull();
+ });
+
+ it('spocita celkovy soucet cen produktu v kosiku', function(){
+ basket.getAll = function() {
+ return [
+ {price: 1000, quantity: 10},
+ {price: 500, quantity: 2}
+ ];
+ };
+ expect(basket.priceProducts()).toBe(11000);
+ });
+
+ it('spocita celkovou cenu objednavky', function(){
+ basket.priceProducts = function() {
+ return 11000;
+ };
+ basket.getTransport = function() {
+ return {price: 79};
+ };
+ expect(basket.priceTotal()).toBe(11079);
+ });
+
+});
View
78 test/frontend/unit/BasketStorage.test.js
@@ -1,78 +0,0 @@
-describe('BasketStorage', function(){
-
- var storage;
-
- beforeEach(function(){
- storage = new BasketStorage();
- storage.clear();
- });
-
- it('prida novy produkt s variantou do uloziste', function(){
- storage.add({id: 12345, variant: 'cerny'});
- expect(storage.exist(12345, 'cerny')).toBeTruthy();
- expect(storage.get(12345, 'cerny')).toEqual({id: 12345, variant: 'cerny'});
- });
-
- it('prida novy produkt bez varianty do uloziste', function(){
- storage.add({id: 12345});
- expect(storage.exist(12345)).toBeTruthy();
- expect(storage.get(12345)).toEqual({id: 12345});
- });
-
- it('zmeni mnozstvi produktu s variantou v kosiku', function(){
- storage.add({id: 12345, variant: 'cerny'});
- storage.updateQuantity(10, 12345, 'cerny');
- expect(storage.get(12345, 'cerny').quantity).toEqual(10);
- });
-
- it('zmeni mnozstvi produktu bez varianty v kosiku', function(){
- storage.add({id: 12345});
- storage.updateQuantity(10, 12345);
- expect(storage.get(12345).quantity).toEqual(10);
- });
-
- it('odstrani produkt bez varianty z kosiku', function(){
- storage.add(12345, {});
- storage.remove(12345);
- expect(storage.get(12345)).toBeUndefined();
- });
-
- it('odstrani produkt s variantou z kosiku', function(){
- storage.add({id: 12345, variant: 'cerny'});
- storage.add({id: 12345, variant: 'bily'});
- storage.remove(12345, 'cerny');
- expect(storage.get(12345, 'cerny')).toBeUndefined();
- expect(storage.get(12345, 'bily')).toBeDefined();
- });
-
- it('vrati vsechny produkty v kosiku', function(){
- storage.add({id: 12456, variant: 'cerny'});
- storage.add({id: 12457, variant: 'bily'});
- storage.add({id: 12459, variant: 'modry'});
- expect(storage.getAll()).toEqual([
- {id: 12456, variant: 'cerny'},
- {id: 12457, variant: 'bily'},
- {id: 12459, variant: 'modry'}
- ]);
- });
-
- it('edituje data uzivatele', function(){
- storage.updateCustomer({name: 'Jakub', surname: 'Mrozek'});
- expect(storage.getCustomer()).toEqual({name: 'Jakub', surname: 'Mrozek'});
- });
-
- it('edituje informace o doprave', function(){
- storage.updateTransport({name: 'Doprava ABC'});
- expect(storage.getTransport()).toEqual({name: 'Doprava ABC'});
- });
-
- it('vymaze obsah kosiku', function(){
- storage.add({id: 12456, variant: 'cerny'});
- storage.updateCustomer({name: 'Jakub', surname: 'Mrozek'});
- storage.updateTransport({name: 'Doprava ABC'});
- storage.clear();
- expect(storage.getAll()).toEqual([]);
- expect(storage.getCustomer()).toBeNull();
- expect(storage.getTransport()).toBeNull();
- });
-});
View
4 views/layout.html
@@ -35,7 +35,7 @@
<ul id="navigation" class="nav">
<li>
<a ng-href="/kosik"><i class="icon-shopping-cart icon-white"></i>
- <strong id="nav-basket-price">{{storage.priceProducts() | currency}}</strong></a>
+ <strong id="nav-basket-price">{{basket.priceProducts() | currency}}</strong></a>
</li>
</ul>
</div>
@@ -90,7 +90,7 @@
<script src="/js/directives.js"></script>
<script src="/lib/angular/angular-resource.js"></script>
- <script src="/js/BasketStorage.js"></script>
+ <script src="/js/Basket.js"></script>
<script src="/js/Transport.js"></script>

0 comments on commit 44c297c

Please sign in to comment.
Something went wrong with that request. Please try again.