Add "Gallery" field to return multiple images in 1 post #242

Merged
Vylpes merged 5 commits from feature/161-image-array into develop 2024-10-26 22:00:55 +01:00
3 changed files with 50 additions and 4 deletions
Showing only changes of commit b1adc39a35 - Show all commits

View file

@ -12,10 +12,6 @@ export default class ImageHelper {
const dom = htmlparser.parseDocument(fetched.body); const dom = htmlparser.parseDocument(fetched.body);
const img = htmlparser.DomUtils.findAll((x => x.tagName == "img" && x.attributes.find(y => y.value.includes("https://preview.redd.it")) != null), dom.children); const img = htmlparser.DomUtils.findAll((x => x.tagName == "img" && x.attributes.find(y => y.value.includes("https://preview.redd.it")) != null), dom.children);
if (!img) {
return [];
}
const imgSrc = img const imgSrc = img
.flatMap(x => x.attributes.find(x => x.name == "src")?.value) .flatMap(x => x.attributes.find(x => x.name == "src")?.value)
.filter(x => x != undefined); .filter(x => x != undefined);

View file

@ -30,3 +30,17 @@ Query.Subreddit = rabbits
Query.Sort By = hot Query.Sort By = hot
Query.Limit = 100" Query.Limit = 100"
`; `;
exports[`GenerateOutput GIVEN the Gallery input has more than 1 item, EXPECT Gallery line to be added 1`] = `
"Archived = false
Author = author
Downvotes = 0
Hidden = false
Permalink = /r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/
Subreddit = Rabbits
Subreddit Subscribers = 654751
Title = This is my Ms Bear!
Upvotes = 17
Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d
Gallery = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d, https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d"
`;

View file

@ -105,4 +105,40 @@ describe("GenerateOutput", () => {
// Assert // Assert
expect(result).toMatchSnapshot(); expect(result).toMatchSnapshot();
}); });
test("GIVEN the Gallery input has more than 1 item, EXPECT Gallery line to be added", () => {
// Arrange
const response = {
IsSuccess: true,
Query: {
subreddit: "rabbits",
sortBy: "hot",
limit: 100,
},
Result: {
Archived: false,
Author: 'author',
Downs: 0,
Hidden: false,
Permalink: "/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/",
Subreddit: "Rabbits",
SubredditSubscribers: 654751,
Title: "This is my Ms Bear!",
Ups: 17,
Url: "https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d",
Gallery: [
"https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d",
"https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d"
],
},
} as IReturnResult;
const options = {} as ICliOptions;
// Act
const result = OutputHelper.GenerateOutput(response, options);
// Assert
expect(result).toMatchSnapshot();
});
}); });