<commit_before>var test = require('tape');
var sync = require('../ampersand-sync');
var Model = require('ampersand-model');


function getStub(data) {
    return {
        url: '/library',
        trigger: function () {
            // capture args for comparison
            this.args = arguments;
        },
        toJSON: function () {
            return data || {};
        }
    };
}

test('should allow models to overwrite ajax configs at the model level', function (t) {
    t.plan(3);
    var Me = Model.extend({
        url: '/hi',
        ajaxConfig: {
            useXDR: true,
            xhrFields: {
                withCredentials: true
            }
        }
    });
    var m = new Me();
    m.on('request', function (model, xhr, options, ajaxSettings) {
        t.equal(ajaxSettings.type, 'GET');
        t.equal(ajaxSettings.xhrFields.withCredentials, true);
        t.equal(ajaxSettings.useXDR, true);
        t.end();
    });
    var xhr = sync('read', m);
});

test('read', function (t) {
    var xhr = sync('read', getStub());
    t.equal(xhr.ajaxSettings.url, '/library');
    t.equal(xhr.ajaxSettings.type, 'GET');
    t.ok(!xhr.ajaxSettings.json);
    t.ok(!xhr.ajaxSettings.data);
    var xhr2 = sync('read', getStub(), {url: '/library/books'});
    t.equal(xhr2.ajaxSettings.url, '/library/books', 'passed url should overwrite model url');
    t.end();
});

test('passing data', function (t) {
    // on reads it should be part of the URL
    var xhr = sync('read', getStub(), {data: {a: 'a', one: 1}});
    t.equal(xhr.ajaxSettings.url, '/library?a=a&one=1', 'data passed to reads should be made into a query string');
    var otherStub = getStub();
    otherStub.url = '/library?something=hi';
    var xhr2 = sync('read', otherStub, {data: {a: 'a', one: 1}});
    t.equal(xhr2.ajaxSettings.url, '/library?something=hi&a=a&one=1', 'data passed to reads should be made into a query string');
    var xhr3 = sync('read', getStub(), {url: '/library/books', data: {a: 'a', one: 1}});
    t.equal(xhr3.ajaxSettings.url, '/library/books?a=a&one=1', 'data passed to reads should added to query string for passed url');
    t.end();
});
<commit_msg>Tidy up “read with data” tests to make it a bit more readable.<commit_after>var test = require('tape');
var sync = require('../ampersand-sync');
var Model = require('ampersand-model');


function getStub(data) {
    return {
        url: '/library',
        trigger: function () {
            // capture args for comparison
            this.args = arguments;
        },
        toJSON: function () {
            return data || {};
        }
    };
}

test('should allow models to overwrite ajax configs at the model level', function (t) {
    t.plan(3);
    var Me = Model.extend({
        url: '/hi',
        ajaxConfig: {
            useXDR: true,
            xhrFields: {
                withCredentials: true
            }
        }
    });
    var m = new Me();
    m.on('request', function (model, xhr, options, ajaxSettings) {
        t.equal(ajaxSettings.type, 'GET');
        t.equal(ajaxSettings.xhrFields.withCredentials, true);
        t.equal(ajaxSettings.useXDR, true);
        t.end();
    });
    var xhr = sync('read', m);
});

test('read', function (t) {
    var xhr = sync('read', getStub());
    t.equal(xhr.ajaxSettings.url, '/library');
    t.equal(xhr.ajaxSettings.type, 'GET');
    t.ok(!xhr.ajaxSettings.json);
    t.ok(!xhr.ajaxSettings.data);
    var xhr2 = sync('read', getStub(), {url: '/library/books'});
    t.equal(xhr2.ajaxSettings.url, '/library/books', 'passed url should overwrite model url');
    t.end();
});

test('passing data', function (t) {
    // on reads it should be part of the URL
    var xhr = sync('read', getStub(), {data: {a: 'a', one: 1}});
    t.equal(xhr.ajaxSettings.url, '/library?a=a&one=1', 'data passed to reads should be made into a query string');

    var modelStub = getStub();
    modelStub.url = '/library?something=hi';
    var xhr2 = sync('read', modelStub, {data: {a: 'a', one: 1}});
    t.equal(xhr2.ajaxSettings.url, '/library?something=hi&a=a&one=1', 'data passed to reads should be appended to an existing query string in the url');

    var xhr3 = sync('read', getStub(), {url: '/library/books', data: {a: 'a', one: 1}});
    t.equal(xhr3.ajaxSettings.url, '/library/books?a=a&one=1', 'data passed to reads should be added as a query string to overwritten url');
    t.end();
});

{'fraction_non_alphanumeric': 0.17107344632768362, 'fraction_numerical': 0.005875706214689265, 'mean_word_length': 2.69449081803005, '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': '2702775', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1452, 'n_words': 394}