Skip to content

Commit

Permalink
refactor: add ellipsis to shortened overivew
Browse files Browse the repository at this point in the history
Also improved tests to make it easier to follow what's going on.
  • Loading branch information
treipatru committed Jul 11, 2024
1 parent 24b5606 commit d1604be
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 23 deletions.
69 changes: 54 additions & 15 deletions src/actions/movie/get-movie-info.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,63 @@
import { getMovieInfo } from '@/actions/movie/get-movie-info.js';
import {
getMovieInfo,
getMovieOverview,
} from '@/actions/movie/get-movie-info.js';
import { generateMovie } from '@/fakers/moviedb.js';

test('should return the movie info', () => {
const movie = generateMovie({
releaseDate: new Date('1986-05-16'),
describe('getMovieOverview', () => {
test('should return the overview if it is less than 350 characters', () => {
const overview = 'a'.repeat(349);
const result = getMovieOverview(overview);

expect(result).toBe(overview);
});

const movieUrl = `https://www.themoviedb.org/movie/${movie.id}`;
const result = getMovieInfo(movie);
test('should return the overview truncated to 350 characters', () => {
const overview = 'a'.repeat(351);
const result = getMovieOverview(overview);

expect(result).toContain(movie.overview);
expect(result).toContain(movie.title);
expect(result).toContain(movieUrl);
expect(result).toContain('#movie #film #cinema');
expect(result).toBe('a'.repeat(347) + '...');
});
});

test('should trim the overview to 350 characters', () => {
const movie = generateMovie();
movie.overview = 'a'.repeat(500);
describe('getMovieInfo', () => {
test('contains the movie title and release date', () => {
const movie = generateMovie({
releaseDate: new Date('1986-05-16'),
});
const result = getMovieInfo(movie);

expect(result).toContain(movie.title);
expect(result).toContain('May 16th, 1986');
});

test('contains the entry URL', () => {
const movie = generateMovie();
const result = getMovieInfo(movie);

expect(result).toContain(`https://www.themoviedb.org/movie/${movie.id}`);
});

test('contains post tags', () => {
const movie = generateMovie();
const result = getMovieInfo(movie);

const result = getMovieInfo(movie);
expect(result).toContain('a'.repeat(350));
expect(result).toContain('#movie #film #cinema');
});

test('contains the full overview if it is less than 350 characters', () => {
const movie = generateMovie();
movie.overview = 'a'.repeat(300);
const result = getMovieInfo(movie);

expect(result).toContain(movie.overview);
});

test('contains the truncated overview if it is more than 350 characters', () => {
const movie = generateMovie();
movie.overview = 'a'.repeat(351);
const result = getMovieInfo(movie);

expect(result).toContain('a'.repeat(347) + '...');
});
});
40 changes: 32 additions & 8 deletions src/actions/movie/get-movie-info.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
import { type Movie } from '@/types/moviedb.js';
import { format } from 'date-fns';

/**
* Mastodon has a DEFAULT character limit of 500. It can be changed per
* instance.
*
* There is no 'short' version of the overview in the API so we have to
* approximate a max length for it.
*
* @param {string} overview
* @return {*} {string}
*/
export function getMovieOverview(overview: string): string {
if (overview.length <= 350) {
return overview;
}

return `${overview.substring(0, 347)}...`;
}

/**
* Generate a message with the movie title, release date, overview, and
* a link to the movie's page on The Movie Database.
*
* @export
* @param {Movie} {
* id,
* overview,
* release_date,
* title,
* }
* @return {*}
*/
export function getMovieInfo({
id,
overview,
release_date,
title,
}: Movie) {
const line1 = `${title} (${format(new Date(release_date), 'PPP')})`;
/**
* Mastodon has a DEFAULT character limit of 500. It can be changed per
* instance.
*
* There is no 'short' version of the overview in the API so we have to
* approximate a max length for it.
*/
const line2 = overview.substring(0, 350);
const line2 = getMovieOverview(overview);
const line3 = `https://www.themoviedb.org/movie/${id}`;
const line4 = '#movie #film #cinema';

Expand Down

0 comments on commit d1604be

Please sign in to comment.