{"size": 3052, "ext": "js", "max_stars_repo_path": "assets/src/scripts/services/statistics.spec.js", "max_stars_repo_name": "mbucknell/ngwmn-ui", "max_stars_repo_stars_event_min_datetime": "2019-03-01T04:03:02.000Z", "max_stars_repo_stars_event_max_datetime": "2020-10-07T14:44:57.000Z", "max_issues_repo_path": "assets/src/scripts/services/statistics.spec.js", "max_issues_repo_name": "mbucknell/ngwmn-ui", "max_issues_repo_issues_event_min_datetime": "2018-06-13T16:41:40.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-02T02:44:41.000Z", "max_forks_repo_path": "assets/src/scripts/services/statistics.spec.js", "max_forks_repo_name": "mbucknell/ngwmn-ui", "max_forks_repo_forks_event_min_datetime": "2018-06-21T18:10:54.000Z", "max_forks_repo_forks_event_max_datetime": "2021-07-27T19:21:40.000Z"}
{"max_stars_count": 3, "max_issues_count": 587, "max_forks_count": 10, "avg_line_length": 27.4954954955, "max_line_length": 123, "alphanum_fraction": 0.4619921363}
import { retrieveMedianWaterLevels } from './statistics';

export const MOCK_MEDIAN_WATER_LEVEL_RESPONSE = '{ \
    "monthly":"not used in the test", \
    "overall":"not used in the test", \
    "medians":" 2016-06-10T04:15:00-05:00,43.00,\\n2006-06-10T04:15:00-05:00,22.00,\\n2010-06-10T04:15:00-05:00,20.00, ", \
    "errors":[], \
    "ok":true \
}';

export const MOCK_MEDIAN_WATER_LEVEL_DATA = [
    { 
        'year':'2016',
        'month':'06',
        'median':'43.00'
    },{
        'year':'2006',
        'month':'06',
        'median':'22.00'
    },{
        'year':'2010',
        'month':'06',
        'median':'20.00'
    }
];

export const MOCK_WATER_LEVEL_SAMPLES = {
    'samples': [
        {
            'time': '2016-06-10T04:15:00-05:00',
            'originalValue': '43.00',
            'fromLandsurfaceValue': '16.52',
            'fromDatumValue': '842.5',
            'comment': 'A'
        },
        {
            'time':'2006-06-10T04:15:00-05:00',
            'originalValue': '22.00',
            'fromLandsurfaceValue': '16.60',
            'fromDatumValue': '842.4',
            'comment': 'A'
        },
        {
            'time':'2010-06-10T04:15:00-05:00',
            'originalValue': '20.00',
            'fromLandsurfaceValue': '16.60',
            'fromDatumValue': '842.4',
            'comment': 'A'
        }
    ]
};


describe('statistics service module', () => {
    beforeEach(() => {
        jasmine.Ajax.install();
    });

    afterEach(() => {
        jasmine.Ajax.uninstall();
    });

    describe('retrieveMedianWaterLevels', () => {
        let request;
        let promise;

        beforeEach(() => {
            promise = retrieveMedianWaterLevels(MOCK_WATER_LEVEL_SAMPLES);
            request = jasmine.Ajax.requests.mostRecent();
        });

        it('returns error and empty response on failure', () => {
            request.respondWith({
                status: 500,
                statusText: 'oops my bad'
            });
            promise.then(resp => {
                expect(resp).toEqual({
                    error: true,
                    message: 'Failed with status 500: oops my bad',
                    medians: []
                });
            });
        });

        it('returns correctly parsed JSON', () => {
            request.respondWith({
                status: 200,
                response: MOCK_MEDIAN_WATER_LEVEL_RESPONSE,
                contentType: 'text/xml'
            });
            promise.then(waterLevels => {
                expect(waterLevels.medians).toEqual(MOCK_MEDIAN_WATER_LEVEL_DATA);
            });
        });

        it('sets a status message on null response', () => {
            request.respondWith({
                status: 200
            });
            promise.then(waterLevels => {
                expect(waterLevels).toEqual({
                    error: true,
                    message: 'No water level data available',
                    medians: []
                });
            });
        });
    });
});
