Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/pages/api/v2/guesslang.test.ts
Views: 687
import { createMocks } from "lib/api/test-framework";1import handler from "./guesslang";23describe("/api/v2/guesslang", () => {4test("guess language of some code", async () => {5const { req, res } = createMocks({6method: "POST",7url: "/api/v2/guesslang",8body: {9code: "for i in range(10):\n print(i**2)",10cutoff: 3,11},12});1314await handler(req, res);15expect(res.statusCode).toBe(200);16const data = res._getJSONData();17expect(data.result.length).toBe(3);18expect(data.result[0]).toEqual("py");19});2021test("default number of responses is 5", async () => {22const { req, res } = createMocks({23method: "POST",24url: "/api/v2/guesslang",25body: { code: "for i in range(10):\n print(i**2)" },26});2728await handler(req, res);2930expect(res.statusCode).toBe(200);31const data = res._getJSONData();32expect(data.result.length).toBe(5);33});3435test("error if code not given", async () => {36const { req, res } = createMocks({37method: "POST",38url: "/api/v2/guesslang",39body: {},40});4142await handler(req, res);43expect(res.statusCode).toBe(400);44});4546test("error if code is not a string", async () => {47const { req, res } = createMocks({48method: "POST",49url: "/api/v2/guesslang",50body: { code: 10 },51});5253await handler(req, res);54expect(res.statusCode).toBe(400);55});5657test("no error if extra param", async () => {58const cutoff = 7;59const { req, res } = createMocks({60method: "POST",61url: "/api/v2/guesslang",62body: {63code: "for i in range(10):\n print(i**2)",64foo: "bar",65cutoff,66},67});6869await handler(req, res);70expect(res.statusCode).toBe(200);71const data = res._getJSONData();72expect(data.result.length).toBe(cutoff);73});74});757677