All files / test/data fixtures.js

26.56% Statements 17/64
22.5% Branches 9/40
27.27% Functions 3/11
26.56% Lines 17/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128        3x     3x 3x     1x 1x 1x 1x 1x 1x     1x                                                                       1x               1x     1x                           2x   42x   2x       2x                                                                              
import data from './tasks.json';
import fixture from 'can-fixture';
import DefineList from 'can-define/list/list';
 
let index = 1000;
 
// a mock ajax service
fixture.delay = 200;
fixture({
    'GET /tasks' (params) {
        // eslint-disable-next-line
        console.log('Query REST Params: ',params);
        const perPage = params.data.perPage || 10;
        const page = params.data.page || 0;
        const sortInfo = params.data.sort;
        let totalItems = data.length;
        let tempData = new DefineList(data);
 
        // filter it
        Iif (params.data.filters && params.data.filters.length) {
            // lets just handle one filter for testing
            // eslint-disable-next-line
            console.warn('only the first filter is going to be used!');
            const f = params.data.filters[0];
            const exclusions = [null, '', undefined];
            if (exclusions.indexOf(f.value === -1)) {
                switch (f.operator) {
                case 'equals':
                    tempData = tempData.filter((d) => {
                    // eslint-disable-next-line
                    return d[f.name] == f.value;
                    });
                    break;
                default:
                    if (f.operator !== 'like') {
                        // eslint-disable-next-line
                        console.warn(f.operator, 'operator not implemented in fixture, like will be used instead!');
                    }
                    if (typeof f.value !== 'string') {
                        // eslint-disable-next-line
                        console.warn('ignoring filter on non-string value');
                    } else {
                        tempData = tempData.filter((d) => {
                            return d[f.name].toUpperCase().indexOf(f.value.toUpperCase()) !== -1;
                        });
                    }
                }
                // eslint-disable-next-line
                console.warn('found ' + tempData.length + ' items after filtering');
                totalItems = tempData.length;
            }
        }
 
 
        // sort it
        Iif (sortInfo && sortInfo.field) {
            const field = sortInfo.field;
            tempData = tempData.sort((a, b) => {
                return sortInfo.type === 'asc' ? (a[field] === b[field] ? 0 : a[field] > b[field] ? 1 : -1) : (a[field] === b[field] ? 0 : a[field] > b[field] ? -1 : 1);
            });
        }
 
        // pageinate it
        tempData = tempData.slice(page * perPage, (page + 1) * perPage);
 
        // return the serialized version
        return {
            data: tempData.serialize(),
            total: totalItems
        };
    },
    'POST /tasks' (params, response) {
        const newId = index++;
        const newObj = Object.assign({
            id: newId
        }, params.data);
        data.push(newObj);
        response(data[data.length - 1]);
    },
    'GET /tasks/{id}' (params, response) {
        const items = data.filter((item) => {
            // eslint-disable-next-line eqeqeq
            return item.id == params.data.id;
        });
        Iif (!items.length) {
            response(404, 'Not Found');
            return null;
        }
        return items[0];
    },
    'PUT /tasks/{id}' (params, response) {
        let item = data.filter((i) => {
            // eslint-disable-next-line eqeqeq
            return i.id == params.data.id;
        });
        if (!item.length) {
            response(404, 'Not Found');
            return;
        }
        item = item[0];
        const idx = data.indexOf(item);
        if (idx !== -1) {
            data[idx] = Object.assign(item, params.data);
            response(data);
        } else {
            response(404, 'Not Found');
        }
    },
    'DELETE /tasks/{id}' (params, response) {
        let item = data.filter((i) => {
            // eslint-disable-next-line eqeqeq
            return i.id == params.data.id;
        });
        if (!item.length) {
            response(404, 'Not Found');
            return;
        }
        item = item[0];
        const idx = data.indexOf(item);
        if (idx !== -1) {
            data.splice(data.indexOf(item), 1);
            response(data);
        } else {
            response(404, 'Not Found');
        }
    }
});