Skip to content

chore: add test for error when calling getData() on an in-memory request #1551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
8 changes: 6 additions & 2 deletions packages/request-client.js/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,12 @@ export default class Request {
* @returns The updated request data
*/
public getData(): Types.IRequestDataWithEvents {
if (this.inMemoryInfo) {
return Object.assign(new EventEmitter(), {
...this.inMemoryInfo.requestData,
});
}

if (this.confirmationErrorOccurredAtCreation) {
throw Error('request confirmation failed');
}
Expand All @@ -709,8 +715,6 @@ export default class Request {
requestData = pending as RequestLogicTypes.IRequest;
requestData.state = RequestLogicTypes.STATE.PENDING;
pending = { state: this.pendingData?.state };
} else if (!requestData && !pending) {
return Object.assign(new EventEmitter(), {} as Types.IRequestDataWithEvents);
}

const currency = this.currencyManager.fromStorageCurrency(requestData.currency);
Expand Down
15 changes: 15 additions & 0 deletions packages/request-client.js/test/in-memory-request.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EventEmitter } from 'events';
import { RequestNetwork, RequestNetworkBase } from '../src/index';
import * as TestData from './data-test';

Expand All @@ -10,6 +11,7 @@ import {
NoPersistDataWrite,
PendingStore,
} from '@requestnetwork/data-access';
import { ClientTypes } from '@requestnetwork/types';

class MyCustomDataAccess extends CombinedDataAccess {
constructor() {
Expand Down Expand Up @@ -128,6 +130,19 @@ describe('handle in-memory request', () => {
);
});

it('returns an empty EventEmitter object when calling getData', async () => {
requestNetwork = new RequestNetwork({
skipPersistence: true,
signatureProvider: TestData.fakeSignatureProvider,
});

const request = await requestNetwork.createRequest(requestCreationParams);

expect(request.getData()).toStrictEqual(
Object.assign(new EventEmitter(), {} as ClientTypes.IRequestDataWithEvents),
);
});

it('persists a previously created in-memory request', async () => {
requestNetwork = new RequestNetwork({
skipPersistence: true,
Expand Down