48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import request from "supertest"
|
|
import app from "./app"
|
|
import { createSubscription, updateSubscription } from "./data/subscription"
|
|
|
|
const pushSubscription = {
|
|
endpoint: "https://updates.push.services.mozilla.com/wpush/v2/aaaaaaa",
|
|
expirationTime: null,
|
|
keys: {
|
|
auth: "adfsadfasdf",
|
|
p256dh: "aaaaaaaaaaaa",
|
|
},
|
|
}
|
|
|
|
jest.mock("./data/subscription")
|
|
|
|
const mockedCreateSubscription = jest.mocked(createSubscription)
|
|
const mockedUpdateSubscription = jest.mocked(updateSubscription)
|
|
|
|
describe("/api/subscription", () => {
|
|
beforeEach(() => {
|
|
mockedCreateSubscription.mockImplementation((subscription) =>
|
|
Promise.resolve({ id: 40, subscription })
|
|
)
|
|
|
|
mockedUpdateSubscription.mockImplementation((subscription, id) =>
|
|
Promise.resolve({ id, subscription })
|
|
)
|
|
})
|
|
|
|
test("POST", async () => {
|
|
const response = await request(app)
|
|
.post("/api/subscription/")
|
|
.send(pushSubscription)
|
|
|
|
expect(JSON.parse(response.text)).toEqual({
|
|
subscriptionId: 40,
|
|
})
|
|
|
|
expect(mockedCreateSubscription).toHaveBeenCalledWith(pushSubscription)
|
|
})
|
|
|
|
test("PUT", async () => {
|
|
await request(app).put("/api/subscription/10/").send(pushSubscription)
|
|
|
|
expect(mockedUpdateSubscription).toHaveBeenCalledWith(pushSubscription, 10)
|
|
})
|
|
})
|