mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
Passing jest test, not all requests stubbed out
This commit is contained in:
parent
d7d2c2ef06
commit
75743cae32
1545
backend/src/jest/snapshots/embeds/babyLovesCat.html
Normal file
1545
backend/src/jest/snapshots/embeds/babyLovesCat.html
Normal file
File diff suppressed because one or more lines are too long
@ -1,15 +1,39 @@
|
||||
import fetch, { Response } from 'node-fetch'
|
||||
import fetch from 'node-fetch'
|
||||
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { createTestClient } from 'apollo-server-testing'
|
||||
import createServer from '../../server'
|
||||
import { gql } from '../../jest/helpers'
|
||||
|
||||
jest.mock('node-fetch')
|
||||
const { Response } = jest.requireActual('node-fetch')
|
||||
|
||||
let variables = {}
|
||||
|
||||
const babyLovesCat = fs.readFileSync(
|
||||
path.join(__dirname, '../../jest/snapshots/embeds/babyLovesCat.html'),
|
||||
'utf8',
|
||||
)
|
||||
|
||||
const babyLovesCatEmbedResponse = new Response(
|
||||
JSON.stringify({
|
||||
height: 270,
|
||||
provider_name: 'YouTube',
|
||||
title: 'Baby Loves Cat',
|
||||
type: 'video',
|
||||
width: 480,
|
||||
thumbnail_height: 360,
|
||||
provider_url: 'https://www.youtube.com/',
|
||||
thumbnail_width: 480,
|
||||
html:
|
||||
'<iframe width="480" height="270" src="https://www.youtube.com/embed/qkdXAtO40Fo?start=18&feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>',
|
||||
thumbnail_url: 'https://i.ytimg.com/vi/qkdXAtO40Fo/hqdefault.jpg',
|
||||
version: '1.0',
|
||||
author_name: 'Merkley Family',
|
||||
author_url: 'https://www.youtube.com/channel/UC5P8yei950tif7UmdPpkJLQ',
|
||||
}),
|
||||
)
|
||||
|
||||
describe('Query', () => {
|
||||
describe('embed', () => {
|
||||
let embedAction
|
||||
@ -36,6 +60,7 @@ describe('Query', () => {
|
||||
lang
|
||||
logo
|
||||
sources
|
||||
html
|
||||
}
|
||||
}
|
||||
`
|
||||
@ -45,17 +70,9 @@ describe('Query', () => {
|
||||
|
||||
describe('given a youtube link', () => {
|
||||
beforeEach(() => {
|
||||
const youtubeHtml = fs.readFileSync(
|
||||
path.join(__dirname, './embeds/snapshots/babyLovesCat.html'),
|
||||
'utf8',
|
||||
)
|
||||
const embedJson = fs.readFileSync(
|
||||
path.join(__dirname, './embeds/snapshots/oembed/babyLovesCat.json'),
|
||||
'utf8',
|
||||
)
|
||||
fetch
|
||||
.mockReturnValueOnce(Promise.resolve(new Response(youtubeHtml)))
|
||||
.mockReturnValueOnce(Promise.resolve(new Response(embedJson)))
|
||||
.mockReturnValueOnce(Promise.resolve(new Response(babyLovesCat)))
|
||||
.mockReturnValueOnce(Promise.resolve(babyLovesCatEmbedResponse))
|
||||
variables = { url: 'https://www.youtube.com/watch?v=qkdXAtO40Fo&t=18s' }
|
||||
})
|
||||
|
||||
@ -63,7 +80,7 @@ describe('Query', () => {
|
||||
const expected = expect.objectContaining({
|
||||
data: {
|
||||
embed: {
|
||||
type: 'link',
|
||||
type: 'video',
|
||||
title: 'Baby Loves Cat',
|
||||
author: 'Merkley Family',
|
||||
publisher: 'YouTube',
|
||||
@ -75,8 +92,10 @@ describe('Query', () => {
|
||||
audio: null,
|
||||
video: null,
|
||||
lang: 'de',
|
||||
logo: 'https://www.youtube.com/yts/img/favicon_144-vfliLAfaB.png',
|
||||
sources: ['resource'],
|
||||
logo: 'https://www.youtube.com/favicon.ico',
|
||||
sources: ['resource', 'oembed'],
|
||||
html:
|
||||
'<iframe width="480" height="270" src="https://www.youtube.com/embed/qkdXAtO40Fo?start=18&feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>',
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
@ -38,9 +38,8 @@ oEmbedProvidersFile = oEmbedProvidersFile.replace('{format}', 'json')
|
||||
const oEmbedProviders = JSON.parse(oEmbedProvidersFile)
|
||||
|
||||
const fetchEmbed = async targetUrl => {
|
||||
const url = new URL(targetUrl)
|
||||
const provider = oEmbedProviders.find(provider => {
|
||||
return provider.provider_url.includes(url.hostname)
|
||||
const provider = oEmbedProviders.find(provider => {
|
||||
return provider.provider_url.includes(targetUrl.hostname)
|
||||
})
|
||||
if (!provider) return {}
|
||||
const {
|
||||
@ -50,13 +49,14 @@ const fetchEmbed = async targetUrl => {
|
||||
endpointUrl.searchParams.append('url', targetUrl)
|
||||
endpointUrl.searchParams.append('format', 'json')
|
||||
const response = await fetch(endpointUrl)
|
||||
const json = await response.json()
|
||||
const {
|
||||
type = 'link',
|
||||
html,
|
||||
author_name, // eslint-disable-line camelcase
|
||||
upload_date, // eslint-disable-line camelcase
|
||||
sources = ['oembed'],
|
||||
} = await response.json()
|
||||
} = json
|
||||
|
||||
return { type, html, author: author_name, date: upload_date, sources }
|
||||
}
|
||||
@ -66,13 +66,15 @@ const fetchMeta = async targetUrl => {
|
||||
const html = await response.text()
|
||||
const metadata = await metascraper({ html, url: targetUrl })
|
||||
|
||||
metadata.sources = ['resource']
|
||||
metadata.type = 'link'
|
||||
|
||||
return metadata
|
||||
return {
|
||||
sources: ['resource'],
|
||||
type: 'link',
|
||||
...metadata,
|
||||
}
|
||||
}
|
||||
|
||||
export default async function scrape(targetUrl) {
|
||||
targetUrl = new URL(targetUrl)
|
||||
if (targetUrl.hostname === 'youtu.be') {
|
||||
// replace youtu.be to get proper results
|
||||
targetUrl.hostname = 'youtube.com'
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -1 +0,0 @@
|
||||
{"type":"video","provider_url":"https:\/\/www.youtube.com\/","thumbnail_url":"https:\/\/i.ytimg.com\/vi\/qkdXAtO40Fo\/hqdefault.jpg","author_name":"Merkley Family","title":"Baby Loves Cat","thumbnail_width":480,"width":480,"version":"1.0","html":"\u003ciframe width=\"480\" height=\"270\" src=\"https:\/\/www.youtube.com\/embed\/qkdXAtO40Fo?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen\u003e\u003c\/iframe\u003e","thumbnail_height":360,"height":270,"author_url":"https:\/\/www.youtube.com\/channel\/UC5P8yei950tif7UmdPpkJLQ","provider_name":"YouTube"}
|
||||
Loading…
x
Reference in New Issue
Block a user