Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTrunk committed Jan 28, 2025
1 parent a17bd25 commit 8f9eda5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 50 deletions.
26 changes: 16 additions & 10 deletions tests/unit/actionApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ const { expect, assert } = chai;

const reqValid = {
params: {
id: 141,
id: 'bc1walletidentity',
},
query: {
id: 141,
id: 'bc1walletidentity',
},
};

Expand All @@ -25,24 +25,28 @@ describe('Action API', function () {
});

// Testing using stub data
it('should return successful result 141 if stub value is valid', async function () {
it('should return successful result bc1walletidentity if stub value is valid', async function () {
const request = httpMocks.createRequest({
method: 'GET',
url: 'test',
body: reqValid,
query: { id: 141 },
query: { id: 'bc1walletidentity' },
});
const res = httpMocks.createResponse({
eventEmiiter: require('events').EventEmitter,
req: request,
});
await sinon.stub(actionService, 'getAction').returns({ wkIdentity: 141 });
await sinon
.stub(actionService, 'getAction')
.returns({ wkIdentity: 'bc1walletidentity' });
await actionApi.getAction(request, res);
expect(JSON.parse(res._getData())).to.have.property('wkIdentity');
expect(JSON.parse(res._getData())).to.deep.equal({ wkIdentity: 141 });
expect(JSON.parse(res._getData())).to.deep.equal({
wkIdentity: 'bc1walletidentity',
});
});

it('should return Bad Request result 141 if stub value is invalid', async function () {
it('should return Bad Request result c1walletidentityif stub value is invalid', async function () {
const request = httpMocks.createRequest({
method: 'GET',
url: 'test',
Expand All @@ -52,17 +56,19 @@ describe('Action API', function () {
eventEmiiter: require('events').EventEmitter,
req: request,
});
await sinon.stub(actionService, 'getAction').returns({ wkIdentity: 141 });
await sinon
.stub(actionService, 'getAction')
.returns({ wkIdentity: 'bc1walletidentity' });
await actionApi.getAction(request, res);
expect(res._getData()).to.deep.equal('Bad Request');
});

it('should return error result 141 if stub value is valid', async function () {
it('should return error result bc1walletidentity if stub value is valid', async function () {
const request = httpMocks.createRequest({
method: 'GET',
url: 'test',
body: reqValid,
query: { id: 141 },
query: { id: 'bc1walletidentity' },
});
const res = httpMocks.createResponse({
eventEmiiter: require('events').EventEmitter,
Expand Down
76 changes: 44 additions & 32 deletions tests/unit/actionService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ const { expect, assert } = chai;

const testInsert = [
{
wkIdentity: 141,
wkIdentity: 'bc1walletidentity',
},
{
wkIdentity: 121,
wkIdentity: 'bc1walletidentitya',
},
{
wkIdentity: 231,
wkIdentity: 'bc1walletidentity3',
},
];

Expand Down Expand Up @@ -48,78 +48,90 @@ describe('Action Service', function () {

it('should return error when id is not valid', async function () {
await actionService
.getAction(123)
.catch((e) => assert.equal(e, 'Error: Action 123 not found'));
.getAction('bc1walletidentity123')
.catch((e) =>
assert.equal(e, 'Error: Action bc1walletidentity123 not found'),
);
});

it('should return data when id is valid', async function () {
await database.collection(actionCollection).insertMany(testInsert);
await actionService.getAction(141).then((r) => {
await actionService.getAction('bc1walletidentity').then((r) => {
expect(r).to.have.property('wkIdentity');
expect(r).to.deep.equal({ wkIdentity: 141 });
expect(r).to.deep.equal({ wkIdentity: 'bc1walletidentity' });
});
});

it('should return error after database drop and id is invalid', async function () {
await actionService
.getAction(141)
.catch((e) => assert.equal(e, 'Error: Action 141 not found'));
.getAction('bc1walletidentity')
.catch((e) =>
assert.equal(e, 'Error: Action bc1walletidentity not found'),
);
});

// Testing using stub data
it('should return successful result 141 if stub value is valid', async function () {
it('should return successful result bc1walletidentity if stub value is valid', async function () {
await sinon
.stub(serviceHelper, 'findOneInDatabase')
.returns({ wkIdentity: 141 });
await actionService.getAction(141).then((r) => {
.returns({ wkIdentity: 'bc1walletidentity' });
await actionService.getAction('bc1walletidentity').then((r) => {
expect(r).to.have.property('wkIdentity');
expect(r).to.deep.equal({ wkIdentity: 141 });
expect(r).to.deep.equal({ wkIdentity: 'bc1walletidentity' });
});
});

it('should return successful result 121 if stub value is valid', async function () {
it('should return successful result bc1walletidentitya if stub value is valid', async function () {
await sinon
.stub(serviceHelper, 'findOneInDatabase')
.returns({ wkIdentity: 121 });
await actionService.getAction(121).then((r) => {
.returns({ wkIdentity: 'bc1walletidentitya' });
await actionService.getAction('bc1walletidentitya').then((r) => {
expect(r).to.have.property('wkIdentity');
expect(r).to.deep.equal({ wkIdentity: 121 });
expect(r).to.deep.equal({ wkIdentity: 'bc1walletidentitya' });
});
});

it('should return error result if stub value is false', async function () {
await sinon.stub(serviceHelper, 'findOneInDatabase').returns(false);
await actionService
.getAction(141)
.catch((e) => assert.equal(e, 'Error: Action 141 not found'));
.getAction('bc1walletidentity141')
.catch((e) =>
assert.equal(e, 'Error: Action bc1walletidentity141 not found'),
);
});

it('should return error result if stub value is undefined', async function () {
await sinon.stub(serviceHelper, 'findOneInDatabase').returns(undefined);
await actionService
.getAction(141)
.catch((e) => assert.equal(e, 'Error: Action 141 not found'));
.getAction('bc1walletidentity141')
.catch((e) =>
assert.equal(e, 'Error: Action bc1walletidentity141 not found'),
);
});

it('should return error result if stub value is null', async function () {
await sinon.stub(serviceHelper, 'findOneInDatabase').returns(undefined);
await actionService
.getAction(141)
.catch((e) => assert.equal(e, 'Error: Action 141 not found'));
.getAction('bc1walletidentity141')
.catch((e) =>
assert.equal(e, 'Error: Action bc1walletidentity141 not found'),
);
});
});

describe('Post Action: Correctly verifies action', function () {
it('should return data with wkIdentity when data is valid', async function () {
await actionService.postAction({ wkIdentity: 144 }).then((r) => {
expect(r).to.have.property('createdAt');
expect(r).to.have.property('expireAt');
expect(r.createdAt).to.not.be.null;
expect(r.createdAt).to.not.be.undefined;
expect(r.expireAt).to.not.be.null;
expect(r.expireAt).to.not.be.undefined;
expect(r.wkIdentity).equal(144);
});
await actionService
.postAction({ wkIdentity: 'bc1walletidentityb' })
.then((r) => {
expect(r).to.have.property('createdAt');
expect(r).to.have.property('expireAt');
expect(r.createdAt).to.not.be.null;
expect(r.createdAt).to.not.be.undefined;
expect(r.expireAt).to.not.be.null;
expect(r.expireAt).to.not.be.undefined;
expect(r.wkIdentity).equal('bc1walletidentityb');
});
});

it('should return data without wkIdentity when data is empty', async function () {
Expand Down
20 changes: 12 additions & 8 deletions tests/unit/syncApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ const { expect, assert } = chai;

const reqValid = {
params: {
id: 141,
id: 'bc1walletidentity141',
},
query: {
id: 141,
id: 'bc1walletidentity141',
},
};

Expand All @@ -25,21 +25,25 @@ describe('Sync API', function () {
});

// Testing using stub data
it('should return successful result 141 if stub value is valid', async function () {
it('should return successful result bc1walletidentity141 if stub value is valid', async function () {
const request = httpMocks.createRequest({
method: 'GET',
url: 'test',
body: reqValid,
query: { id: 141 },
query: { id: 'bc1walletidentity141' },
});
const res = httpMocks.createResponse({
eventEmiiter: require('events').EventEmitter,
req: request,
});
await sinon.stub(syncService, 'getSync').returns({ wkIdentity: 141 });
await sinon
.stub(syncService, 'getSync')
.returns({ wkIdentity: 'bc1walletidentity141' });
await syncApi.getSync(request, res);
expect(JSON.parse(res._getData())).to.have.property('wkIdentity');
expect(JSON.parse(res._getData())).to.deep.equal({ wkIdentity: 141 });
expect(JSON.parse(res._getData())).to.deep.equal({
wkIdentity: 'bc1walletidentity141',
});
});

it('should return Bad Request result 141 if stub value is false', async function () {
Expand All @@ -57,7 +61,7 @@ describe('Sync API', function () {
expect(res._getData()).to.deep.equal('Bad Request');
});

it('should return Bad Request result 141 if stub value is undefined', async function () {
it('should return Bad Request result bc1walletidentity141 if stub value is undefined', async function () {
const request = httpMocks.createRequest({
method: 'GET',
url: 'test',
Expand All @@ -72,7 +76,7 @@ describe('Sync API', function () {
expect(res._getData()).to.deep.equal('Bad Request');
});

it('should return Bad Request result 141 if stub value is null', async function () {
it('should return Bad Request result bc1walletidentity141 if stub value is null', async function () {
const request = httpMocks.createRequest({
method: 'GET',
url: 'test',
Expand Down

0 comments on commit 8f9eda5

Please sign in to comment.