-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stef -- Carets #32
base: master
Are you sure you want to change the base?
Stef -- Carets #32
Changes from all commits
62f9465
6701682
3c91903
3d71ed7
11a4a3b
5183778
4958383
d0b1d9d
79fbbb4
249a504
d6cfc70
570d60f
95d7cc7
ce83f10
4359396
86caafb
c5ea3df
8445492
03225f4
dbaba17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import Order from 'models/order'; | ||
|
||
describe('Order spec', () => { | ||
|
||
describe('Initialize', () => { | ||
it('Can access all instance variables.', () => { | ||
const order = new Order({ | ||
currentQuote: 'HELLO', | ||
currentPrice: 100.00, | ||
buy: true, | ||
targetPrice: 101.00 | ||
}); | ||
|
||
expect(order.get('currentQuote')).toEqual('HELLO'); | ||
expect(order.get('currentPrice')).toEqual(100.00); | ||
expect(order.get('buy')).toEqual(true); | ||
expect(order.get('targetPrice')).toEqual(101.00); | ||
}); | ||
}); | ||
|
||
describe('Creates a new Order.', () => { | ||
|
||
it('Creates a valid instance of Order', () => { | ||
const order = new Order({ | ||
currentQuote: 'HELLO', | ||
currentPrice: 100.00, | ||
buy: true, | ||
targetPrice: 50.00, | ||
}); | ||
|
||
expect(order.isValid()).toEqual(true); | ||
}); | ||
|
||
it ('Rejects an empty price.', () => { | ||
const order = new Order({ | ||
symbol: 'HELLO', | ||
currentPrice: '', | ||
action: true, | ||
}); | ||
|
||
expect(order.isValid()).toEqual(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validation failing |
||
}); | ||
|
||
it('Can not create an order with target price greater than or equal to the current price when buying.', () => { | ||
const order = new Order({ | ||
currentQuote: 'HELLO', | ||
currentPrice: 100.00, | ||
buy: true, | ||
targetPrice: 101.00, | ||
}); | ||
|
||
expect(order.isValid()).toEqual(false); | ||
}); | ||
|
||
it('Can not create an order with target price less than or equal to the current price when selling.', () => { | ||
const order = new Order({ | ||
currentQuote: 'HELLO', | ||
currentPrice: 100.00, | ||
buy: false, | ||
targetPrice: 99.00, | ||
}); | ||
|
||
expect(order.isValid()).toEqual(false); | ||
}); | ||
|
||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Backbone from 'backbone'; | ||
import Order from 'models/order'; | ||
|
||
const OrderList = Backbone.Collection.extend({ | ||
model: Order, | ||
}); | ||
|
||
export default OrderList; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import Backbone from 'backbone'; | ||
|
||
const Order = Backbone.Model.extend({ | ||
initialize(params) { | ||
this.buy = params.buy, | ||
this.currentQuote = params.currentQuote, | ||
this.currentPrice = params.currentPrice, | ||
this.targetPrice = params.targetPrice | ||
}, | ||
|
||
validate(attributes) { | ||
const error = {}; | ||
if (this.buy && this.targetPrice >= this.currentPrice) { | ||
error.targetPrice = ['Price is higher than market, aim lower!']; | ||
} | ||
else if (!this.buy && this.targetPrice <= this.currentPrice) { | ||
error.targetPrice = ['Price is lower than market, aim higher!']; | ||
} | ||
else if (!attributes.targetPrice) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You also need (based on your tests) to check for numeric value for price, boolean value for |
||
error.targetPrice = ['Price must not be blank!']; | ||
} | ||
|
||
if (Object.keys(error).length < 1) { | ||
return error; | ||
} else { | ||
return false; | ||
} | ||
}, | ||
}); | ||
|
||
export default Order; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import _ from 'underscore'; | ||
import $ from 'jquery'; | ||
|
||
import Backbone from 'backbone'; | ||
|
||
import OrderView from '../views/order_view'; | ||
import Order from '../models/order' | ||
|
||
const OrderListView = Backbone.View.extend({ | ||
initialize(params) { | ||
this.template = params.template; | ||
this.quoteList = params.quoteList; | ||
this.listenTo(this.model, 'update', this.render); | ||
}, | ||
|
||
render() { | ||
this.$('#orders').empty(); | ||
this.model.each((order) => { | ||
const orderView = new OrderView({ | ||
model: order, | ||
template: this.template, | ||
tagName: 'li', | ||
className: 'order', | ||
}); | ||
this.$('#orders').append(orderView.render().$el); | ||
}); | ||
return this; | ||
}, | ||
|
||
events: { | ||
'click button.btn-buy': 'buyOrder', | ||
'click button.btn-sell': 'sellOrder' | ||
}, | ||
|
||
buyOrder: function(event) { | ||
event.preventDefault(); | ||
const orderData = { | ||
buy: true, | ||
symbol: this.$('select[name=symbol]').val(), | ||
targetPrice: parseFloat(this.$('input[name=price-target]').val()), | ||
currentPrice: 0, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is the order to get the current quote price? Maybe this view needs to have an instance variable referring to the |
||
currentQuote: 0 | ||
}; | ||
|
||
const newOrder = new Order(orderData); | ||
|
||
if (newOrder.isValid()) { | ||
this.model.add(newOrder); | ||
} else { | ||
console.log('Invalid Order!', newOrder.validationError); | ||
} | ||
}, | ||
|
||
sellOrder: function(event) { | ||
event.preventDefault(); | ||
|
||
const orderData = { | ||
buy: false, | ||
symbol: this.$('select[name=symbol]').val(), | ||
targetPrice: parseFloat(this.$('input[name=price-target]').val()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No currentPrice |
||
}; | ||
|
||
const newOrder = new Order(orderData); | ||
|
||
if (newOrder.isValid()) { | ||
this.model.add(newOrder); | ||
} else { | ||
console.log('Invalid Order!'); | ||
} | ||
}, | ||
|
||
|
||
}); | ||
|
||
export default OrderListView; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import $ from 'jquery'; | ||
import _ from 'underscore'; | ||
|
||
import Backbone from 'backbone'; | ||
|
||
import Order from '../models/order'; | ||
|
||
const OrderView = Backbone.View.extend({ | ||
initialize(params) { | ||
this.template = params.template; | ||
this.orderTemplate = params.orderTemplate; | ||
this.listenTo(this.model, "change", this.render); | ||
}, | ||
|
||
render() { | ||
const compiledTemplate = this.template(this.model.toJSON()); | ||
this.$el.html(compiledTemplate); | ||
return this; | ||
}, | ||
|
||
events: { | ||
'click button.btn-cancel': 'cancelOrder', | ||
}, | ||
|
||
cancelOrder(){ | ||
this.model.destroy(); | ||
}, | ||
}); | ||
|
||
export default OrderView; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import _ from 'underscore'; | ||
import Backbone from 'backbone'; | ||
|
||
import QuoteView from '../views/quote_view'; | ||
import Quote from '../models/quote'; | ||
|
||
const QuoteListView = Backbone.View.extend({ | ||
initialize(params) { | ||
this.template = params.template; | ||
this.listenTo(this.model, 'update', this.render); | ||
}, | ||
|
||
render() { | ||
this.$('#quotes').empty(); | ||
this.model.each((quote) => { | ||
const quoteView = new QuoteView({ | ||
model: quote, | ||
template: this.template, | ||
tagName: 'li', | ||
className: 'quote', | ||
}); | ||
this.$('#quotes').append(quoteView.render().$el); | ||
}); | ||
return this; | ||
}, | ||
}); | ||
|
||
export default QuoteListView; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import $ from 'jquery'; | ||
import _ from 'underscore'; | ||
|
||
import Backbone from 'backbone'; | ||
import Quote from '../models/quote'; | ||
|
||
const QuoteView = Backbone.View.extend({ | ||
initialize(params) { | ||
this.template = params.template; | ||
this.listenTo(this.model, "change", this.render); | ||
}, | ||
|
||
// This is the portion of the code, that needs to be listening to the buttons for click events, as it is rendering the buttons on the page. | ||
render() { | ||
const compiledTemplate = this.template(this.model.toJSON()); | ||
this.$el.html(compiledTemplate); | ||
|
||
return this; | ||
}, | ||
|
||
events: { | ||
'click button.btn-buy': 'buyQuote', | ||
'click button.btn-sell': 'sellQuote', | ||
}, | ||
|
||
buyQuote: function() { | ||
this.model.set('buy', true); | ||
let tradeTemplate = _.template($('#trade-template').html()); | ||
$('#trades').prepend(tradeTemplate(this.model.attributes)); | ||
this.model.buy(); | ||
}, | ||
|
||
sellQuote: function() { | ||
this.model.set('buy', false); | ||
let tradeTemplate = _.template($('#trade-template').html()); | ||
$('#trades').prepend(tradeTemplate(this.model.attributes)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should not directly access jQuery in a view. Instead use |
||
this.model.sell(); | ||
}, | ||
}); | ||
|
||
export default QuoteView; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validation failing