|
| 1 | +import * as chai from "chai"; |
| 2 | +import * as nock from "nock"; |
| 3 | +import * as chaiAsPromised from "chai-as-promised"; |
| 4 | + |
| 5 | +import { getSampleCrash } from "./getSampleCrash"; |
| 6 | +import { FirebaseError } from "../error"; |
| 7 | +import { crashlyticsApiOrigin } from "../api"; |
| 8 | + |
| 9 | +chai.use(chaiAsPromised); |
| 10 | +const expect = chai.expect; |
| 11 | + |
| 12 | +describe("getSampleCrash", () => { |
| 13 | + const appId = "1:1234567890:android:abcdef1234567890"; |
| 14 | + const requestProjectNumber = "1234567890"; |
| 15 | + const issueId = "test_issue_id"; |
| 16 | + const variantId = "test_variant_id"; |
| 17 | + const sampleCount = 10; |
| 18 | + |
| 19 | + afterEach(() => { |
| 20 | + nock.cleanAll(); |
| 21 | + }); |
| 22 | + |
| 23 | + it("should resolve with the response body on success", async () => { |
| 24 | + const mockResponse = { events: [{ event_id: "1" }] }; |
| 25 | + |
| 26 | + nock(crashlyticsApiOrigin()) |
| 27 | + .get(`/v1alpha/projects/${requestProjectNumber}/apps/${appId}/events`) |
| 28 | + .query({ |
| 29 | + "filter.issue.id": issueId, |
| 30 | + page_size: String(sampleCount), |
| 31 | + }) |
| 32 | + .reply(200, mockResponse); |
| 33 | + |
| 34 | + const result = await getSampleCrash(appId, issueId, sampleCount, undefined); |
| 35 | + |
| 36 | + expect(result).to.deep.equal(mockResponse); |
| 37 | + expect(nock.isDone()).to.be.true; |
| 38 | + }); |
| 39 | + |
| 40 | + it("should resolve with the response body on success with variantId", async () => { |
| 41 | + const mockResponse = { events: [{ event_id: "1" }] }; |
| 42 | + |
| 43 | + nock(crashlyticsApiOrigin()) |
| 44 | + .get(`/v1alpha/projects/${requestProjectNumber}/apps/${appId}/events`) |
| 45 | + .query({ |
| 46 | + "filter.issue.id": issueId, |
| 47 | + "filter.issue.variant_id": variantId, |
| 48 | + page_size: String(sampleCount), |
| 49 | + }) |
| 50 | + .reply(200, mockResponse); |
| 51 | + |
| 52 | + const result = await getSampleCrash(appId, issueId, sampleCount, variantId); |
| 53 | + |
| 54 | + expect(result).to.deep.equal(mockResponse); |
| 55 | + expect(nock.isDone()).to.be.true; |
| 56 | + }); |
| 57 | + |
| 58 | + it("should throw a FirebaseError if the API call fails", async () => { |
| 59 | + nock(crashlyticsApiOrigin()) |
| 60 | + .get(`/v1alpha/projects/${requestProjectNumber}/apps/${appId}/events`) |
| 61 | + .query({ |
| 62 | + "filter.issue.id": issueId, |
| 63 | + page_size: String(sampleCount), |
| 64 | + }) |
| 65 | + .reply(500, { error: "Internal Server Error" }); |
| 66 | + |
| 67 | + await expect(getSampleCrash(appId, issueId, sampleCount, undefined)).to.be.rejectedWith( |
| 68 | + FirebaseError, |
| 69 | + /Failed to fetch the same crash/, |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should throw a FirebaseError if the appId is invalid", async () => { |
| 74 | + const invalidAppId = "invalid-app-id"; |
| 75 | + |
| 76 | + await expect(getSampleCrash(invalidAppId, issueId, sampleCount, undefined)).to.be.rejectedWith( |
| 77 | + FirebaseError, |
| 78 | + "Unable to get the projectId from the AppId.", |
| 79 | + ); |
| 80 | + }); |
| 81 | +}); |
0 commit comments