2023-08-18 15:45:01 +08:00
import { jest } from "@jest/globals" ;
import "@testing-library/jest-dom" ;
import axios from "axios" ;
import MockAdapter from "axios-mock-adapter" ;
import { expect , it , describe , afterEach } from "@jest/globals" ;
import { renderGistCard } from "../src/cards/gist-card.js" ;
2023-08-27 15:55:02 +08:00
import { renderError , CONSTANTS } from "../src/common/utils.js" ;
2023-08-18 15:45:01 +08:00
import gist from "../api/gist.js" ;
const gist _data = {
data : {
viewer : {
gist : {
description :
"List of countries and territories in English and Spanish: name, continent, capital, dial code, country codes, TLD, and area in sq km. Lista de países y territorios en Inglés y Español: nombre, continente, capital, código de teléfono, códigos de país, dominio y área en km cuadrados. Updated 2023" ,
owner : {
login : "Yizack" ,
} ,
stargazerCount : 33 ,
forks : {
totalCount : 11 ,
} ,
files : [
{
name : "countries.json" ,
language : {
name : "JSON" ,
} ,
size : 85858 ,
} ,
] ,
} ,
} ,
} ,
} ;
2023-08-21 15:07:47 +08:00
const gist _not _found _data = {
data : {
viewer : {
gist : null ,
} ,
} ,
} ;
2023-08-18 15:45:01 +08:00
const mock = new MockAdapter ( axios ) ;
afterEach ( ( ) => {
mock . reset ( ) ;
} ) ;
describe ( "Test /api/gist" , ( ) => {
it ( "should test the request" , async ( ) => {
const req = {
query : {
id : "bbfce31e0217a3689c8d961a356cb10d" ,
} ,
} ;
const res = {
setHeader : jest . fn ( ) ,
send : jest . fn ( ) ,
} ;
mock . onPost ( "https://api.github.com/graphql" ) . reply ( 200 , gist _data ) ;
await gist ( req , res ) ;
expect ( res . setHeader ) . toBeCalledWith ( "Content-Type" , "image/svg+xml" ) ;
expect ( res . send ) . toBeCalledWith (
renderGistCard ( {
name : gist _data . data . viewer . gist . files [ 0 ] . name ,
nameWithOwner : ` ${ gist _data . data . viewer . gist . owner . login } / ${ gist _data . data . viewer . gist . files [ 0 ] . name } ` ,
description : gist _data . data . viewer . gist . description ,
language : gist _data . data . viewer . gist . files [ 0 ] . language . name ,
starsCount : gist _data . data . viewer . gist . stargazerCount ,
forksCount : gist _data . data . viewer . gist . forks . totalCount ,
} ) ,
) ;
} ) ;
it ( "should get the query options" , async ( ) => {
const req = {
query : {
2023-08-19 14:35:52 +08:00
id : "bbfce31e0217a3689c8d961a356cb10d" ,
2023-08-18 15:45:01 +08:00
title _color : "fff" ,
icon _color : "fff" ,
text _color : "fff" ,
bg _color : "fff" ,
show _owner : true ,
} ,
} ;
const res = {
setHeader : jest . fn ( ) ,
send : jest . fn ( ) ,
} ;
mock . onPost ( "https://api.github.com/graphql" ) . reply ( 200 , gist _data ) ;
await gist ( req , res ) ;
expect ( res . setHeader ) . toBeCalledWith ( "Content-Type" , "image/svg+xml" ) ;
expect ( res . send ) . toBeCalledWith (
renderGistCard (
{
name : gist _data . data . viewer . gist . files [ 0 ] . name ,
nameWithOwner : ` ${ gist _data . data . viewer . gist . owner . login } / ${ gist _data . data . viewer . gist . files [ 0 ] . name } ` ,
description : gist _data . data . viewer . gist . description ,
language : gist _data . data . viewer . gist . files [ 0 ] . language . name ,
starsCount : gist _data . data . viewer . gist . stargazerCount ,
forksCount : gist _data . data . viewer . gist . forks . totalCount ,
} ,
{ ... req . query } ,
) ,
) ;
} ) ;
2023-08-20 15:58:48 +08:00
it ( "should render error if id is not provided" , async ( ) => {
const req = {
query : { } ,
} ;
const res = {
setHeader : jest . fn ( ) ,
send : jest . fn ( ) ,
} ;
await gist ( req , res ) ;
expect ( res . setHeader ) . toBeCalledWith ( "Content-Type" , "image/svg+xml" ) ;
expect ( res . send ) . toBeCalledWith (
renderError (
'Missing params "id" make sure you pass the parameters in URL' ,
"/api/gist?id=GIST_ID" ,
) ,
) ;
} ) ;
2023-08-21 15:07:47 +08:00
it ( "should render error if gist is not found" , async ( ) => {
const req = {
query : {
id : "bbfce31e0217a3689c8d961a356cb10d" ,
} ,
} ;
const res = {
setHeader : jest . fn ( ) ,
send : jest . fn ( ) ,
} ;
mock
. onPost ( "https://api.github.com/graphql" )
. reply ( 200 , gist _not _found _data ) ;
await gist ( req , res ) ;
expect ( res . setHeader ) . toBeCalledWith ( "Content-Type" , "image/svg+xml" ) ;
expect ( res . send ) . toBeCalledWith ( renderError ( "Gist not found" ) ) ;
} ) ;
2023-08-22 15:03:59 +08:00
it ( "should render error if wrong locale is provided" , async ( ) => {
const req = {
query : {
id : "bbfce31e0217a3689c8d961a356cb10d" ,
locale : "asdf" ,
} ,
} ;
const res = {
setHeader : jest . fn ( ) ,
send : jest . fn ( ) ,
} ;
await gist ( req , res ) ;
expect ( res . setHeader ) . toBeCalledWith ( "Content-Type" , "image/svg+xml" ) ;
expect ( res . send ) . toBeCalledWith (
renderError ( "Something went wrong" , "Language not found" ) ,
) ;
} ) ;
2023-08-27 15:55:02 +08:00
it ( "should have proper cache" , async ( ) => {
const req = {
query : {
id : "bbfce31e0217a3689c8d961a356cb10d" ,
} ,
} ;
const res = {
setHeader : jest . fn ( ) ,
send : jest . fn ( ) ,
} ;
mock . onPost ( "https://api.github.com/graphql" ) . reply ( 200 , gist _data ) ;
await gist ( req , res ) ;
expect ( res . setHeader ) . toBeCalledWith ( "Content-Type" , "image/svg+xml" ) ;
expect ( res . setHeader ) . toBeCalledWith (
"Cache-Control" ,
` max-age= ${ CONSTANTS . FOUR _HOURS / 2 } , s-maxage= ${
CONSTANTS . FOUR _HOURS
} , stale - while - revalidate = $ { CONSTANTS . ONE _DAY } ` ,
) ;
} ) ;
2023-08-18 15:45:01 +08:00
} ) ;