const { expect, knex, describe, beforeEach, afterEach, it, sinon } = require('../../../test-helper');
const snapshotRepository = require('../../../../lib/infrastructure/repositories/snapshot-repository');
const Snapshot = require('../../../../lib/domain/models/data/snapshot');

describe('Unit | Repository | SnapshotRepository', function() {

  const serializedUserProfile = {
    data: {
      type: 'users',
      id: 'user_id',
      attributes: {
        'first-name': 'Luke',
        'last-name': 'Skywalker',
        'total-pix-score': 128,
        'email': 'luke@sky.fr'
      },
      relationships: {
        competences: {
          data: [
            { type: 'competences', id: 'recCompA' },
            { type: 'competences', id: 'recCompB' }
          ]
        }
      },
    },
    included: [
      {
        type: 'areas',
        id: 'recAreaA',
        attributes: {
          name: 'area-name-1'
        }
      },
      {
        type: 'areas',
        id: 'recAreaB',
        attributes: {
          name: 'area-name-2'
        }
      },
      {
        type: 'competences',
        id: 'recCompA',
        attributes: {
          name: 'competence-name-1',
          index: '1.1',
          level: -1,
          'course-id': 'recBxPAuEPlTgt72q11'
        },
        relationships: {
          area: {
            data: {
              type: 'areas',
              id: 'recAreaA'
            }
          }
        }
      },
      {
        type: 'competences',
        id: 'recCompB',
        attributes: {
          name: 'competence-name-2',
          index: '1.2',
          level: 8,
          'pix-score': 128,
          'course-id': 'recBxPAuEPlTgt72q99'
        },
        relationships: {
          area: {
            data: {
              type: 'areas',
              id: 'recAreaB'
            }
          }
        }
      }
    ]
  };
  const snapshot = {
    organizationId: 150,
    score: 128,
    profile: JSON.stringify(serializedUserProfile)
  };

  beforeEach(() => {
    sinon.spy(Snapshot.prototype, 'save');
  });

  afterEach(() => {
    Snapshot.prototype.save.restore();
    knex('snapshots').delete();
  });

  it('should save a new snapshot', () => {
    // given

    const promise = snapshotRepository.save(snapshot);

    return promise.then(snapshotId => {
      expect(snapshotId).to.not.be.null;
      sinon.assert.calledOnce(Snapshot.prototype.save);
    });
  });
});

@@ -47,16 +47,23 @@
 terEach,
+ after,
  it, sin
@@ -1993,16 +1993,78 @@
 )%0A  %7D;%0A%0A
+  after(() =%3E %7B%0A    return knex('snapshots').delete();%0A  %7D);%0A%0A
   before

Add knex delete to snapshot repository