Chai to test JSON API output

Viewed 2247

How can I use Chai to deep parse JSON API output?

JSON API Output:

{
    "data": {
        "id": "87zc08spyq69",
        "type": "data-collector",
        "attributes": {
            "sensors": [
                {
                    "metadata": {
                        "sensor_id": 837490,
                        "unit": "m",
                        "unit_description": "meters",
                        "datum": "WGS84",
                        "offset": -0.001
                    },
                    "data": [
                        {
                            "time": "2017-12-31T23:59:59Z",
                            "value": "14.9",
                            "quality": 4
                        },
                        {
                            "time": "2017-12-31T22:59:59Z",
                            "value": "12",
                            "quality": 1
                        },
                        {
                            "time": "2017-12-31T21:59:59Z",
                            "value": "10",
                            "quality": 2
                        }
                    ]
                }
            ]
        }
    },
    "links": {
        "self": "/locations/87zc08spyq69/counter/time-series"
    }
}

Working So Far:

const chai = require('chai');
const should = chai.should();
const chaiHttp = require('chai-http');
const chaiThings = require('chai-things');
chai.use(chaiHttp);
chai.use(chaiThings);

uri = '/locations/87zc08spyq69/counter/time-series';
describe('/GET', () => {
  it('should get location counter in time-series', done => {
    chai.request(server)
        .get(uri)
        .set('Content-Type', "application/vnd.api+json")
        .set('Accept', "application/vnd.api+json")
        .end((err, res) => {
            if (err) {
               console.error(err);
               done(err,null);
            }
            should.not.exist(err);
            res.status.should.eql(200);
            res.type.should.eql("application/vnd.api+json");
            res.body.should.have.property('data').has.property('attributes')
                                                 .has.property('sensors')
                                                 .to.be.an('array')
                                                 .should.contain.an('object');

            res.body.should.have.property('links').has.property('self').to.be.eql(uri);
            done();
        }); 
  });
});

I am unable to test any part of the following array:

"data": [

                {
                    "time": "2017-12-31T23:59:59Z",
                    "value": "14.9",
                    "quality": 4
                },
                {
                    "time": "2017-12-31T22:59:59Z",
                    "value": "12",
                    "quality": 1
                },
                {
                    "time": "2017-12-31T21:59:59Z",
                    "value": "10",
                    "quality": 2
                }
            ]

I have tried chai-things to "deep" test "data", "data/time", "data/value" but I keep getting errors.

How can I test sensors/data, sensors/data/time, sensors/data/value?

Thanks in advance for your help

1 Answers

Its pretty ugly, and there may be a better way to do it, but this worked for me

res.body.should.have.property('data').have.property('attributes')
  .have.property('sensors')
  .have.deep.property('[0].data').have.deep.property('[0].time').eql("2017-12-31T23:59:59Z");
Related