Skip to content

Commit

Permalink
Merge pull request #54 from JHWelch/image-sizing
Browse files Browse the repository at this point in the history
Image sizing
  • Loading branch information
JHWelch authored Oct 4, 2023
2 parents aa1c797 + 29d9e3e commit be2604d
Show file tree
Hide file tree
Showing 20 changed files with 245 additions and 45 deletions.
34 changes: 34 additions & 0 deletions __tests__/config/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Config from '../../src/config/config'
beforeEach(() => {
jest.resetModules()
process.env = {
NODE_ENV: 'production',
NOTION_TOKEN: 'NOTION_TOKEN',
DATABASE_ID: 'DATABASE_ID',
PORT: '3000',
Expand All @@ -19,6 +20,7 @@ describe('env variables all present', () => {
it('returns the config', () => {
const config = new Config()

expect(config.nodeEnv).toBe('production')
expect(config.notionToken).toBe('NOTION_TOKEN')
expect(config.notionDatabaseId).toBe('DATABASE_ID')
expect(config.port).toBe(3000)
Expand Down Expand Up @@ -99,3 +101,35 @@ describe('env missing CALENDAR_URL', () => {
expect(() => new Config()).toThrowError('CALENDAR_URL is missing')
})
})

describe('env missing NODE_ENV', () => {
beforeEach(() => {
delete process.env.NODE_ENV
})

it('defaults to development', () => {
expect(new Config().nodeEnv).toBe('development')
})
})

describe('isProduction', () => {
describe('when NODE_ENV is production', () => {
beforeEach(() => {
process.env.NODE_ENV = 'production'
})

it('returns true', () => {
expect(new Config().isProduction).toBe(true)
})
})

describe('when NODE_ENV is development', () => {
beforeEach(() => {
process.env.NODE_ENV = 'development'
})

it('returns false', () => {
expect(new Config().isProduction).toBe(false)
})
})
})
4 changes: 2 additions & 2 deletions __tests__/controllers/cacheController.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('cache', () => {
2001,
90,
'https://www.themoviedb.org/movie/1234',
'https://image.tmdb.org/t/p/original/poster.jpg',
'/poster.jpg',
1234,
'notionId',
)
Expand All @@ -108,7 +108,7 @@ describe('cache', () => {
2001,
90,
'https://www.themoviedb.org/movie/1234',
'https://image.tmdb.org/t/p/original/poster.jpg',
'/poster.jpg',
1234,
)
const notionResponse = new NotionMovie('notionId', 'title')
Expand Down
32 changes: 30 additions & 2 deletions __tests__/data/firestore/firestoreAdapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,34 @@ describe('cacheWeeks', () => {
)
})
})

describe('when mode is development', () => {
it('uses the development collection', async () => {
firestore = new FirestoreAdapter(mockConfig({ nodeEnv: 'development' }))

await firestore.cacheWeeks([
new Week('id1', 'theme1', new Date('2021-01-01')),
new Week('id2', 'theme2', new Date('2021-01-08')),
new Week('id3', 'theme3', new Date('2021-01-15')),
])

expect(transaction.set)
.toHaveBeenCalledWith(
FirebaseMock.mockDoc('weeks-dev', '2021-01-01'),
FirebaseMock.mockWeek('id1', 'theme1', '2021-01-01')
)
expect(transaction.set)
.toHaveBeenCalledWith(
FirebaseMock.mockDoc('weeks-dev', '2021-01-08'),
FirebaseMock.mockWeek('id2', 'theme2', '2021-01-08')
)
expect(transaction.set)
.toHaveBeenCalledWith(
FirebaseMock.mockDoc('weeks-dev', '2021-01-15'),
FirebaseMock.mockWeek('id3', 'theme3', '2021-01-15')
)
})
})
})

describe('createRsvp', () => {
Expand Down Expand Up @@ -254,7 +282,7 @@ describe('sendEmailTemplate', () => {
title: 'test title',
year: '2021',
time: '6:00pm',
posterUrl: 'https://example.com/poster.jpg',
posterPath: 'https://example.com/poster.jpg',
},
],
})
Expand All @@ -273,7 +301,7 @@ describe('sendEmailTemplate', () => {
title: 'test title',
year: '2021',
time: '6:00pm',
posterUrl: 'https://example.com/poster.jpg',
posterPath: 'https://example.com/poster.jpg',
},
],
},
Expand Down
2 changes: 1 addition & 1 deletion __tests__/data/notion/notionAdapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('getMovie', () => {
year: 2021,
length: 120,
url: 'movieUrl',
posterUrl: 'moviePosterUrl',
posterPath: 'moviePosterPath',
theaterName: 'movieTheaterName',
showingUrl: 'movieShowingUrl',
tmdbId: null,
Expand Down
2 changes: 1 addition & 1 deletion __tests__/data/tmdb/tmdbAdapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('getMovie', () => {
2001,
90,
'https://www.themoviedb.org/movie/1234',
'https://image.tmdb.org/t/p/original/posterPath.jpg',
'/posterPath.jpg',
1234,
)
tmdbMock.mockSearchMovie(expected)
Expand Down
63 changes: 62 additions & 1 deletion __tests__/models/movie.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { describe, expect, it, test } from '@jest/globals'
import Movie from '../../src/models/movie'
import { MovieFactory } from '../support/factories/movieFactory'
import { TMDB_POSTER_URL } from '../../src/data/tmdb/constants'

describe('merge', () => {
test('only null/undefined fields are overwritten by merge', () => {
Expand Down Expand Up @@ -51,7 +53,7 @@ describe('toNotion', () => {
Year: { number: movie.year },
'Length (mins)': { number: movie.length },
URL: { url: movie.url },
Poster: { url: movie.posterUrl },
Poster: { url: movie.posterPath },
'Theater Name': { rich_text: [
{ text: { content: movie.theaterName } },
] },
Expand Down Expand Up @@ -80,3 +82,62 @@ describe('toNotion', () => {
})
})
})

describe('toDTO', () => {
it('should return a DTO', () => {
const movie = new MovieFactory().make()

expect(movie.toDTO()).toEqual({
title: movie.title,
director: movie.director,
year: movie.year,
length: movie.length,
url: movie.url,
posterUrl: movie.posterUrl(),
theaterName: movie.theaterName,
showingUrl: movie.showingUrl,
isFieldTrip: true,
displayLength: movie.displayLength(),
})
})

describe('No showingUrl or theaterName', () => {
it('marks isFieldTrip as false', () => {
const movie = new MovieFactory().state({
showingUrl: null,
theaterName: null,
}).make()

expect(movie.toDTO()).toEqual({
title: movie.title,
director: movie.director,
year: movie.year,
length: movie.length,
url: movie.url,
posterUrl: movie.posterUrl(),
theaterName: movie.theaterName,
showingUrl: movie.showingUrl,
isFieldTrip: false,
displayLength: movie.displayLength(),
})
})
})
})

describe('posterUrl', () => {
it('appends the posterPath to the base url', () => {
const movie = new MovieFactory().make()

expect(movie.posterUrl()).toEqual(
`${TMDB_POSTER_URL}${movie.posterPath}`
)
})

describe('posterPath is null', () => {
it('returns an empty string', () => {
const movie = new MovieFactory().state({ posterPath: null }).make()

expect(movie.posterUrl()).toEqual('')
})
})
})
36 changes: 36 additions & 0 deletions __tests__/support/factories/movieFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Movie from '../../../src/models/movie'

export class MovieFactory {
private _state = {
title: 'Movie Title',
director: 'Movie Director',
year: 2021,
length: 90,
url: 'https://example.com/movie1234',
tmdbId: 1234,
posterPath: '/path/to/poster.jpg',
notionId: 'notionId',
theaterName: 'Theater',
showingUrl: 'Showing Url',
}

make (): Movie {
return new Movie(
this._state.title,
this._state.director,
this._state.year,
this._state.length,
this._state.url,
this._state.posterPath,
this._state.tmdbId,
this._state.notionId,
this._state.theaterName,
this._state.showingUrl,
)
}

state (state: Partial<typeof this.state>): MovieFactory {
this._state = { ...this._state, ...state }
return this
}
}
5 changes: 4 additions & 1 deletion __tests__/support/mockConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Config from '../../src/config/config.js'

export function mockConfig (): Config {
export function mockConfig ({
nodeEnv = 'production',
} = {}): Config {
process.env = {
NODE_ENV: nodeEnv,
NOTION_TOKEN: 'NOTION_TOKEN',
DATABASE_ID: 'DATABASE_ID',
PORT: '3000',
Expand Down
6 changes: 3 additions & 3 deletions __tests__/support/notionHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class NotionMovie {
public year: number | null = null,
public length: number | null = null,
public url: string | null = null,
public posterUrl: string | null = null,
public posterPath: string | null = null,
public theaterName: string | null = null,
public showingUrl: string | null = null,
) {}
Expand All @@ -166,7 +166,7 @@ export class NotionMovie {
Year: nNumber(this.year),
'Length (mins)': nNumber(this.length),
URL: nUrl(this.url),
Poster: nUrl(this.posterUrl),
Poster: nUrl(this.posterPath),
'Theater Name': nRichText(this.theaterName),
'Showing URL': nUrl(this.showingUrl),
})
Expand All @@ -180,7 +180,7 @@ export class NotionMovie {
2021,
120,
'movieUrl',
'moviePosterUrl',
'moviePosterPath',
'movieTheaterName',
'movieShowingUrl'
)
Expand Down
5 changes: 2 additions & 3 deletions __tests__/support/tmdbMock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Movie from '../../src/models/movie'
import { TMDB_POSTER_URL } from '../../src/data/tmdb/constants'
import { MockFetch } from './fetchMock'

export class TmdbMock {
Expand All @@ -15,7 +14,7 @@ export class TmdbMock {
{
id: id,
original_title: movie.title,
poster_path: movie.posterUrl?.replace(TMDB_POSTER_URL, ''),
poster_path: movie.posterPath,
release_date: `${movie.year}-07-19`,
title: movie.title,
adult: false,
Expand All @@ -39,7 +38,7 @@ export class TmdbMock {
.mockImplementationOnce(async () => new Response(JSON.stringify({
id: id,
original_title: movie.title,
poster_path: movie.posterUrl?.replace(TMDB_POSTER_URL, ''),
poster_path: movie.posterPath,
release_date: `${movie.year}-07-19`,
title: movie.title,
adult: false,
Expand Down
2 changes: 1 addition & 1 deletion emails/reminder.mjml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
>{{ movie.title }}</mj-text>

<mj-image
src="{{ movie.posterUrl }}"
src="{{ movie.posterPath }}"
alt="{{ movie.title }} Poster"
align="center"
width="220px"
Expand Down
2 changes: 1 addition & 1 deletion emails/rsvpConfirmation.mjml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
>{{ movie.title }}</mj-text>

<mj-image
src="{{ movie.posterUrl }}"
src="{{ movie.posterPath }}"
alt="{{ movie.title }} Poster"
align="center"
width="220px"
Expand Down
6 changes: 6 additions & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default class Config {
notionToken: string
port: number
tmdbApiKey: string
nodeEnv: string

constructor () {
this.adminEmail = this.requiredVariable('ADMIN_EMAIL')
Expand All @@ -15,6 +16,11 @@ export default class Config {
this.notionToken = this.requiredVariable('NOTION_TOKEN')
this.port = parseInt(this.optionalVariable('PORT', '8080'))
this.tmdbApiKey = this.requiredVariable('TMDB_READ_KEY')
this.nodeEnv = this.optionalVariable('NODE_ENV', 'development')
}

get isProduction (): boolean {
return this.nodeEnv === 'production'
}

requiredVariable (name: string): string {
Expand Down
Loading

0 comments on commit be2604d

Please sign in to comment.