Skip to content

Commit

Permalink
Refactor StorageDialog tests
Browse files Browse the repository at this point in the history
Tests were failing since the shallow-render library was automatically
stubbing out our mock (which we didn't want to do!). To fix this, add a
`dontMock` call to AccountService and use a provider instead.

Since these tests are being modified, refactor them a bit. Split the
"update account after submiting the form" into two tests for each of the
spies and assertions, and delete the "should format in MB" delete since
all it was testing was that our mocks were being properly injected into
the component; something that is tested in the rest of our tests.

Also replace the usage of the `spyOn` function with built in
functionality of our testing doubles. We already have a mocked/stubbed
out version of both ApiService and AccountService for these tests, so
it's a bit unusual to create a spy of an already existing testing
double. Convert the mock objects to classes that are instantiated in the
beforeEach block so that tests have direct access to the whole objects
and they are reset for each test.

PER-9349: Storage Redeemed Does Not Show In Storage Bar
  • Loading branch information
meisekimiu authored and crisnicandrei committed Oct 20, 2023
1 parent e6d9d2e commit b96f0e7
Showing 1 changed file with 38 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { AccountVO } from '../../../models/account-vo';
import { MessageService } from '../../../shared/services/message/message.service';
import { StorageDialogComponent } from './storage-dialog.component';

const mockPromoData = {
const mockPromoResponse = {
Results: [
{
data: [
Expand All @@ -27,80 +27,71 @@ const mockPromoData = {
isSuccessful: true,
};

const mockApiService = {
billing: {
redeemPromoCode: (value: PromoVOData): Promise<BillingResponse> => {
return Promise.resolve(new BillingResponse(mockPromoData));
},
},
};

const mockAccountService = {
refreshAccount: (): Promise<void> => {
class MockBillingRepo {
public calledRedeemPromoCode = false;
public redeemPromoCode(_value: PromoVOData): Promise<BillingResponse> {
this.calledRedeemPromoCode = true;
return Promise.resolve(new BillingResponse(mockPromoResponse));
}
}

interface MockApiService {
billing: MockBillingRepo;
}
class MockAccountService {
public addedStorage: number | undefined;
public refreshAccount(): Promise<void> {
return Promise.resolve();
},
setAccount: (account: AccountVO): void => {},
getAccount: (): AccountVO => {
}
public setAccount(_account: AccountVO): void {}
public getAccount(): AccountVO {
return new AccountVO({ spaceLeft: 10000, spaceTotal: 10000 });
},
addStorageBytes: (sizeInBytes): void => {},
};
}
public addStorageBytes(sizeInBytes: number): void {
this.addedStorage = sizeInBytes;
}
}

describe('StorageDialogComponent', () => {
let shallow: Shallow<StorageDialogComponent>;
let messageShown: boolean = false;
let mockAccountService: MockAccountService;
let mockApiService: MockApiService;
const dialogRef = new DialogRef(1, null);

beforeEach(() => {
mockAccountService = new MockAccountService();
mockApiService = { billing: new MockBillingRepo() };
shallow = new Shallow(StorageDialogComponent, CoreModule)
.mock(ApiService, mockApiService)
.dontMock(AccountService)
.dontMock(ApiService)
.mock(DialogRef, dialogRef)
.mock(AccountService, mockAccountService)
.mock(MessageService, {
showError: () => {
messageShown = true;
},
});
})
.provide({ provide: AccountService, useValue: mockAccountService })
.provide({ provide: ApiService, useValue: mockApiService });
});

it('should exist', async () => {
const { element } = await shallow.render();
expect(element).not.toBeNull();
});

it('should return the correct size in MB', async () => {
const { inject } = await shallow.render();
const apiService = inject(ApiService);
const accountService = inject(AccountService);
const account = new AccountVO({ spaceLeft: 10000, spaceTotal: 10000 });
accountService.setAccount(account);
const promoVOData: PromoVOData = { code: 'promo4' };
const response = await apiService.billing.redeemPromoCode(promoVOData);
expect(response.getPromoVO().sizeInMB).toBe(5000);
it('should fetch dialog information from the API', async () => {
const { instance } = await shallow.render();
const promoData: PromoVOData = { code: 'promo' };
await instance.onPromoFormSubmit(promoData);
expect(mockApiService.billing.calledRedeemPromoCode).toBeTruthy();
});

it('should update the account after submititng the form ', async () => {
const { instance } = await shallow.render();

const redeemPromoCodeSpy = spyOn(
mockApiService.billing,
'redeemPromoCode'
).and.resolveTo(new BillingResponse(mockPromoData));
const addStorageSpy = spyOn(mockAccountService, 'addStorageBytes').and.callThrough();

const promoData: PromoVOData = { code: 'promo' };

await instance.onPromoFormSubmit(promoData);

expect(redeemPromoCodeSpy).toHaveBeenCalled();

const response = await redeemPromoCodeSpy.calls.mostRecent().returnValue;

const promo = response.getPromoVO();

expect(addStorageSpy).toHaveBeenCalledWith(
promo.sizeInMB * 1024 * 1024
);
expect(mockAccountService.addedStorage).toBe(5000 * 1024 * 1024);
});

it('should enable the button after adding a promo code', async () => {
Expand Down

0 comments on commit b96f0e7

Please sign in to comment.