<commit_before>import Ember from 'ember';
import { module, test } from 'qunit';
import sinonTest from 'dummy/tests/ember-sinon-qunit/test';

import FetchAllRouteMixin from 'ember-osf/mixins/fetch-all-route';

module('Unit | Mixin | fetch all route' //, {

    // TODO: Once tests working, refactor to use before and beforeEach
    //
    // before() {
    //     this.routeClass =
    // },
    // beforeEach() {
    //
    // }}
);




function callModelHook(route) {
    var model;
    Ember.run(() => {
        route.model().then(result => {
            model = result;
        });
    });
}

// Replace this with your real tests.
test('it works', function(assert) {
    let FetchAllObject = Ember.Object.extend(FetchAllRouteMixin);
    let subject = FetchAllObject.create();
    assert.ok(subject);
});


sinonTest('Auto-fetches multiple pages of results when expected', function(assert) {

    let FetchAllRoute = Ember.Route.extend(FetchAllRouteMixin, {
        fakeStorage: Ember.A([1,2,3,4]), // infinity doesn't start pushing objects until page 2, so mimic page 1 being in storage

        model() {
            return this.infinityModel('some_model', {
                modelPath: 'fakeStorage',
                _storeFindMethod: function() {

                    return Ember.RSVP.resolve(
                        Ember.ArrayProxy.create({
                            content: [1, 2, 3, 4],
                            meta: {
                                total: 3
                            }
                    }));
                }
            });
        },

        infinityModelLoaded({totalPages}) {
            console.log('get assertive', ...arguments);
            let storage = this.get('fakeStorage');
            console.log(storage);

            assert.equal(totalPages, 3);
            assert.equal(storage.length, 12);
        }
    });

    let subject = FetchAllRoute.create();
    callModelHook(subject);
});



// TODO
// Things I'd like to test...
// 1. Mock the fetch operation. Confirm that if we expect 3 pages of results, then a function will fire 3x by end of test (will need to block test execution until done?)
// 2. Confirm the modelPath variable contains the expected number of results when done
// 3. Test configuration of relationship fetching works as expected
<commit_msg>Cleanup in progress<commit_after>import Ember from 'ember';
import { module, test } from 'qunit';

import FetchAllRouteMixin from 'ember-osf/mixins/fetch-all-route';

module('Unit | Mixin | fetch all route', {

    // TODO: Once tests working, refactor to use before and beforeEach

    before() {
        this.routeClass = 1;
    },
    beforeEach() {

    }}
);




function callModelHook(route) {
    var model;
    Ember.run(() => {
        route.model().then(result => {
            model = result;
        });
    });
}

function mockQuery() {
    return Ember.RSVP.resolve(
        Ember.ArrayProxy.create({
            content: [1, 2, 3, 4],
            meta: {
                total: 3
            }
        })
    );
}

// Replace this with your real tests.
test('it works', function(assert) {
    let FetchAllObject = Ember.Object.extend(FetchAllRouteMixin);
    let subject = FetchAllObject.create();
    assert.ok(subject);
});


test('Auto-fetches multiple pages of results when expected', function(assert) {

    let FetchAllRoute = Ember.Route.extend(FetchAllRouteMixin, {
        fakeStorage: null,

        model() {
            return this.infinityModel('some_model', {
                modelPath: 'fakeStorage',
                _storeFindMethod: mockQuery
            });
        },

        infinityModelLoaded({totalPages}) {
            let storage = this.get('fakeStorage');
            assert.equal(totalPages, 3);
            assert.equal(storage.length, 12);
        }
    });

    let subject = FetchAllRoute.create({
        'fakeStorage': Ember.A([1,2,3,4])  // infinity doesn't start pushing objects until page 2, so prefill storage with page 1
    });
    callModelHook(subject);
});



// TODO
// Things I'd like to test...
// 1.[x] Mock the fetch operation. Confirm that if we expect 3 pages of results, then a function will fire 3x by end of test (will need to block test execution until done?)
// 2. [x] Confirm the modelPath variable contains the expected number of results when done
// 3. Test configuration of relationship fetching works as expected

{'fraction_non_alphanumeric': 0.11698027658127409, 'fraction_numerical': 0.0088415325323056, 'mean_word_length': 2.1246458923512748, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '5819392', 'n_tokens_mistral': 1283, 'n_tokens_neox': 1203, 'n_words': 361}