<commit_before>'use strict';


var assert = require('assert'),
    Product = require('../src/product');


describe('Product Model', function () {

    var item;


    beforeEach(function () {
        item = new Product({
            name: 'Foo',
            desc: 'This is an item, foo',
            amount: 1.23
        });
    });


    afterEach(function () {
        item = null;
    });


    it('get() returns valid data', function () {
        assert.equal(item.get('name'), 'Foo');
        assert.equal(item.get('desc'), 'This is an item, foo');
        assert.equal(item.get('amount'), 1.23);
    });


    it('set() writes data', function () {
        item.set('name', 'Bar');
        item.set('desc', 'Where is the bar?');
        item.set('amount', 4.56);
        item.set('something', 'random');

        assert.equal(item.get('name'), 'Bar');
        assert.equal(item.get('desc'), 'Where is the bar?');
        assert.equal(item.get('amount'), 4.56);
        assert.equal(item.get('something'), 'random');
    });


    it('set() fires an event', function (done) {
        item.on('change', function (key) {
            assert.equal(key, 'name');
            done();
        });

        item.set('name', 'Baz');
    });


    it('get() returns the correct quantity', function () {
        item.set('quantity', 1234);
        assert.equal(item.get('quantity'), 1234);
    });


    it('get() returns 1 for null quantities', function () {
        assert.equal(item.get('quantity'), 1);
    });


	it('options() returns an array of options', function () {
		var options;

		item.set('on0', 'color');
		item.set('os0', 'blue');
		item.set('on1', 'size');
		item.set('os1', 'large');

		options = item.options();

		assert.equal(options.length, 2);
		assert.deepEqual(options[0], { key: 'color', value: 'blue', amount: 0});
		assert.deepEqual(options[1], { key: 'size', value: 'large', amount: 0});
	});


	it('options() values have an amount', function () {
		var options;

		item.set('on0', 'color');
		item.set('os0', 'blue');
		item.set('on1', 'size');
		item.set('os1', 'large');
		item.set('option_select0', 'blue');
		item.set('option_amount0', 123.00);

		options = item.options();

		assert.deepEqual(options[0].amount, 123.00);
		assert.equal(options[1].amount, 0);
	});


	it('discount() calculates flat rate discounts', function () {
		item.set('discount_amount', 1.00);
		item.set('discount_amount2', 0.50);
		item.set('quantity', 3);

		assert.equal(item.total(), 1.69);
	});


	it('discount() calculates percentage discounts', function () {
		item.set('discount_rate', 25);
		item.set('discount_rate2', 50);
		item.set('quantity', 3);

		assert.equal(item.total(), 2.1525);
	});


    it('total() returns the product total', function () {
        assert.equal(item.total(), 1.23);

        item.set('quantity', 2);
        assert.equal(item.total(), 2.46);

    });


	it('total() takes into account option amounts', function () {
		item.set('on0', 'color');
		item.set('os0', 'blue');
		item.set('option_select0', 'blue');
		item.set('option_amount0', 123.00);

		assert.equal(item.total(), 124.23);

		item.set('quantity', 2);<commit_msg>Product discounts now appear in the UI<commit_after>'use strict';


var assert = require('assert'),
    Product = require('../src/product');


describe('Product Model', function () {

    var item;


    beforeEach(function () {
        item = new Product({
            name: 'Foo',
            desc: 'This is an item, foo',
            amount: 1.23
        });
    });


    afterEach(function () {
        item = null;
    });


    it('get() returns valid data', function () {
        assert.equal(item.get('name'), 'Foo');
        assert.equal(item.get('desc'), 'This is an item, foo');
        assert.equal(item.get('amount'), 1.23);
    });


    it('set() writes data', function () {
        item.set('name', 'Bar');
        item.set('desc', 'Where is the bar?');
        item.set('amount', 4.56);
        item.set('something', 'random');

        assert.equal(item.get('name'), 'Bar');
        assert.equal(item.get('desc'), 'Where is the bar?');
        assert.equal(item.get('amount'), 4.56);
        assert.equal(item.get('something'), 'random');
    });


    it('set() fires an event', function (done) {
        item.on('change', function (key) {
            assert.equal(key, 'name');
            done();
        });

        item.set('name', 'Baz');
    });


    it('get() returns the correct quantity', function () {
        item.set('quantity', 1234);
        assert.equal(item.get('quantity'), 1234);
    });


    it('get() returns 1 for null quantities', function () {
        assert.equal(item.get('quantity'), 1);
    });


	it('options() returns an array of options', function () {
		var options;

		item.set('on0', 'color');
		item.set('os0', 'blue');
		item.set('on1', 'size');
		item.set('os1', 'large');

		options = item.options();

		assert.equal(options.length, 2);
		assert.deepEqual(options[0], { key: 'color', value: 'blue', amount: 0});
		assert.deepEqual(options[1], { key: 'size', value: 'large', amount: 0});
	});


	it('options() values have an amount', function () {
		var options;

		item.set('on0', 'color');
		item.set('os0', 'blue');
		item.set('on1', 'size');
		item.set('os1', 'large');
		item.set('option_select0', 'blue');
		item.set('option_amount0', 123.00);

		options = item.options();

		assert.deepEqual(options[0].amount, 123.00);
		assert.equal(options[1].amount, 0);
	});


	it('discount() calculates flat rate discounts', function () {
		item.set('discount_amount', 1.00);
		item.set('discount_amount2', 0.50);
		item.set('quantity', 3);

		assert.equal(item.total(), 1.69);
	});


	it('discount() calculates percentage discounts', function () {
		item.set('discount_rate', 25);
		item.set('discount_rate2', 50);
		item.set('quantity', 3);

		assert.equal(item.total(), 2.1525);
	});


	it('discount() returns a formatted discount', function () {
		item.set('discount_amount', 1.00);
		item.set('discount_amount2', 0.50);
		item.set('quantity', 3);

		assert.equal(item.discount({ format: true }), 1.69);
	});

    it('total() returns the product total', function () {
        assert.equal(item.total(), 1.23);

        item.set('quantity', 2);
        assert.equal(item.total(), 2.46);

    });


	it('total() takes into account option amounts', function () {
		item.set('on0', 'color');
		item.set('os0', 'blue');
		item.set('option_select0', 'blue');
		item.set('option_amount0', 123.00);

		assert.equal(item.total(), 124.23);

		item.set('quantity', 2);
{'fraction_non_alphanumeric': 0.20981464600425404, 'fraction_numerical': 0.030233971437253114, 'mean_word_length': 3.0312308634415186, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2173580', 'n_tokens_mistral': 2419, 'n_tokens_neox': 2109, 'n_words': 465}