-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(faq): ne pas passer de mapper au strapi service
- Loading branch information
Showing
6 changed files
with
116 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,137 @@ | ||
import { aStrapiCmsRepository } from '~/server/cms/infra/repositories/strapi.repository.fixture'; | ||
import { createSuccess } from '~/server/errors/either'; | ||
import { aListeDeQuestion, aListeFAQSlug, aQuestionEtReponse } from '~/server/faq/domain/FAQ.fixture'; | ||
import { flatMapSlug, mapQuestion, mapQuestionRéponse } from '~/server/faq/infra/strapiFAQ.mapper'; | ||
import { createFailure, createSuccess } from '~/server/errors/either'; | ||
import { ErreurMetier } from '~/server/errors/erreurMetier.types'; | ||
import { aQuestion, aQuestionEtReponse } from '~/server/faq/domain/FAQ.fixture'; | ||
import { aStrapiQuestion, aStrapiQuestionEtReponse, aStrapiQuestionSlug } from '~/server/faq/infra/strapiFAQ.fixture'; | ||
import { StrapiFAQRepository } from '~/server/faq/infra/strapiFAQ.repository'; | ||
|
||
const RESOURCE_FAQ = 'faqs'; | ||
describe('getAllFAQ', () => { | ||
it('appelle le service strapi avec les bons paramètres', async () => { | ||
const strapiCmsRepository = aStrapiCmsRepository(); | ||
const strapiFAQRepository = new StrapiFAQRepository(strapiCmsRepository); | ||
const strapiService = aStrapiCmsRepository(); | ||
jest.spyOn(strapiService, 'getCollectionType').mockResolvedValue(createSuccess([aStrapiQuestion()])); | ||
const strapiFAQRepository = new StrapiFAQRepository(strapiService); | ||
const query= 'fields[0]=problematique&fields[1]=slug'; | ||
|
||
await strapiFAQRepository.getAllFAQ(); | ||
|
||
expect(strapiCmsRepository.getCollectionType).toHaveBeenCalledWith(RESOURCE_FAQ, query, mapQuestion); | ||
expect(strapiService.getCollectionType).toHaveBeenCalledWith(RESOURCE_FAQ, query); | ||
}); | ||
|
||
describe('quand la liste des questions est trouvée', () => { | ||
it('renvoie la liste de questions', async () => { | ||
const expected = aListeDeQuestion(); | ||
const strapiCmsRepository = aStrapiCmsRepository({ getCollectionType: jest.fn() }); | ||
const strapiFAQRepository = new StrapiFAQRepository(strapiCmsRepository); | ||
jest.spyOn(strapiCmsRepository, 'getCollectionType').mockResolvedValue(createSuccess(expected)); | ||
const strapiService = aStrapiCmsRepository(); | ||
const strapiFAQRepository = new StrapiFAQRepository(strapiService); | ||
const strapiQuestions = [ | ||
aStrapiQuestion(), | ||
aStrapiQuestion({ problematique: 'Que faire dans la vie ?', slug: 'que-faire-dans-la-vie' }), | ||
]; | ||
jest.spyOn(strapiService, 'getCollectionType').mockResolvedValue(createSuccess(strapiQuestions)); | ||
const expectedQuestions = createSuccess([ | ||
aQuestion(), | ||
aQuestion({ | ||
problématique: 'Que faire dans la vie ?', | ||
slug: 'que-faire-dans-la-vie', | ||
})]); | ||
|
||
const result = await strapiFAQRepository.getAllFAQ(); | ||
|
||
expect(result).toStrictEqual(expectedQuestions); | ||
}); | ||
}); | ||
|
||
describe('quand la récupération de la liste des questions est en échec', () => { | ||
it('relais l’échec du strapi service', async () => { | ||
const strapiService = aStrapiCmsRepository(); | ||
const strapiFAQRepository = new StrapiFAQRepository(strapiService); | ||
const expectedStrapiFailure = createFailure(ErreurMetier.CONTENU_INDISPONIBLE); | ||
jest.spyOn(strapiService, 'getCollectionType').mockResolvedValue(expectedStrapiFailure); | ||
|
||
const result = await strapiFAQRepository.getAllFAQ(); | ||
|
||
expect(result).toEqual(createSuccess(expected)); | ||
expect(result).toStrictEqual(expectedStrapiFailure); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('getFAQBySlug', () => { | ||
it('appelle le service strapi avec les bons paramètres', async () => { | ||
const strapiCmsRepository = aStrapiCmsRepository(); | ||
const strapiFAQRepository = new StrapiFAQRepository(strapiCmsRepository); | ||
const strapiService = aStrapiCmsRepository(); | ||
jest.spyOn(strapiService, 'getFirstFromCollectionType').mockResolvedValueOnce(createSuccess(aStrapiQuestionEtReponse())); | ||
const strapiFAQRepository = new StrapiFAQRepository(strapiService); | ||
const slug = 'slugName'; | ||
const query= 'filters[slug][$eq]=slugName'; | ||
|
||
await strapiFAQRepository.getFAQBySlug(slug); | ||
|
||
expect(strapiCmsRepository.getFirstFromCollectionType).toHaveBeenCalledWith(RESOURCE_FAQ, query, mapQuestionRéponse); | ||
expect(strapiService.getFirstFromCollectionType).toHaveBeenCalledWith(RESOURCE_FAQ, query); | ||
}); | ||
|
||
describe('quand la question est trouvée', () => { | ||
it('renvoie la question', async () => { | ||
const expected = aQuestionEtReponse(); | ||
const strapiCmsRepository = aStrapiCmsRepository({ getFirstFromCollectionType: jest.fn() }); | ||
const strapiFAQRepository = new StrapiFAQRepository(strapiCmsRepository); | ||
jest.spyOn(strapiCmsRepository, 'getFirstFromCollectionType').mockResolvedValue(createSuccess(expected)); | ||
const strapiService = aStrapiCmsRepository(); | ||
jest.spyOn(strapiService, 'getFirstFromCollectionType').mockResolvedValueOnce(createSuccess(aStrapiQuestionEtReponse())); | ||
const strapiFAQRepository = new StrapiFAQRepository(strapiService); | ||
const expectedQuestionEtReponse = aQuestionEtReponse(); | ||
|
||
const result = await strapiFAQRepository.getFAQBySlug('slugName'); | ||
|
||
expect(result).toStrictEqual(createSuccess(expectedQuestionEtReponse)); | ||
}); | ||
}); | ||
|
||
describe('quand la récupération est en échec', () => { | ||
it('relais l’échec du strapi service', async () => { | ||
const strapiService = aStrapiCmsRepository(); | ||
const expectedFailure = createFailure(ErreurMetier.CONTENU_INDISPONIBLE); | ||
jest.spyOn(strapiService, 'getFirstFromCollectionType').mockResolvedValueOnce(expectedFailure); | ||
const strapiFAQRepository = new StrapiFAQRepository(strapiService); | ||
|
||
const result = await strapiFAQRepository.getFAQBySlug('slugName'); | ||
|
||
expect(result).toEqual(createSuccess(expected)); | ||
expect(result).toStrictEqual(expectedFailure); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('listAllFAQSlug', () => { | ||
it('appelle le service strapi avec les bons paramètres', async () => { | ||
const strapiCmsRepository = aStrapiCmsRepository(); | ||
const strapiFAQRepository = new StrapiFAQRepository(strapiCmsRepository); | ||
const strapiService = aStrapiCmsRepository(); | ||
jest.spyOn(strapiService, 'getCollectionType').mockResolvedValueOnce(createSuccess([aStrapiQuestionSlug()])); | ||
const strapiFAQRepository = new StrapiFAQRepository(strapiService); | ||
const query= '[fields][0]=slug'; | ||
|
||
await strapiFAQRepository.listAllFAQSlug(); | ||
|
||
expect(strapiCmsRepository.getCollectionType).toHaveBeenCalledWith(RESOURCE_FAQ, query, flatMapSlug); | ||
expect(strapiService.getCollectionType).toHaveBeenCalledWith(RESOURCE_FAQ, query); | ||
}); | ||
|
||
describe('quand les slugs sont trouvés', () => { | ||
it('renvoie les slugs', async () => { | ||
const expected = aListeFAQSlug(); | ||
const strapiCmsRepository = aStrapiCmsRepository({ getCollectionType: jest.fn() }); | ||
const strapiCmsRepository = aStrapiCmsRepository(); | ||
const strapiFAQRepository = new StrapiFAQRepository(strapiCmsRepository); | ||
jest.spyOn(strapiCmsRepository, 'getCollectionType').mockResolvedValue(createSuccess(expected)); | ||
jest.spyOn(strapiCmsRepository, 'getCollectionType').mockResolvedValue(createSuccess([ | ||
aStrapiQuestionSlug(), | ||
aStrapiQuestionSlug({ slug: 'comment-constituer-un-dossier-locatif-jeune' }), | ||
])); | ||
const expectedSlugs = ['Comment-constituer-un-dossier-locatif ?', 'comment-constituer-un-dossier-locatif-jeune']; | ||
|
||
const result = await strapiFAQRepository.listAllFAQSlug(); | ||
|
||
expect(result).toStrictEqual(createSuccess(expectedSlugs)); | ||
}); | ||
}); | ||
|
||
describe('quand la récupération des slugs est en échec', () => { | ||
it('relais l’échec du strapi service', async () => { | ||
const strapiService = aStrapiCmsRepository(); | ||
const strapiFAQRepository = new StrapiFAQRepository(strapiService); | ||
const expectFailure = createFailure(ErreurMetier.CONTENU_INDISPONIBLE); | ||
jest.spyOn(strapiService, 'getCollectionType').mockResolvedValue( expectFailure); | ||
|
||
const result = await strapiFAQRepository.listAllFAQSlug(); | ||
|
||
expect(result).toEqual(createSuccess(expected)); | ||
expect(result).toStrictEqual(expectFailure); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,46 @@ | ||
import { StrapiRepository } from '~/server/cms/infra/repositories/strapi.repository'; | ||
import { Either } from '~/server/errors/either'; | ||
import { CmsRepository } from '~/server/cms/domain/cms.repository'; | ||
import { createSuccess, Either, isSuccess } from '~/server/errors/either'; | ||
import { FAQ } from '~/server/faq/domain/FAQ'; | ||
import { FAQRepository } from '~/server/faq/domain/FAQ.repository'; | ||
import { FAQResponseStrapi } from '~/server/faq/infra/strapiFAQ'; | ||
import { flatMapSlug, mapQuestion, mapQuestionRéponse } from '~/server/faq/infra/strapiFAQ.mapper'; | ||
import { mapQuestion, mapQuestionRéponse } from '~/server/faq/infra/strapiFAQ.mapper'; | ||
|
||
const RESOURCE_FAQ = 'faqs'; | ||
|
||
export class StrapiFAQRepository implements FAQRepository { | ||
constructor(private readonly strapiService: StrapiRepository) { | ||
constructor(private readonly strapiService: CmsRepository) { | ||
} | ||
|
||
async getFAQBySlug(slug: string): Promise<Either<FAQ.QuestionEtReponse>> { | ||
const query = `filters[slug][$eq]=${slug}`; | ||
return await this.strapiService.getFirstFromCollectionType<FAQResponseStrapi.QuestionEtReponse, FAQ.QuestionEtReponse>(RESOURCE_FAQ, query, mapQuestionRéponse); | ||
const strapiQuestionEtReponse = await this.strapiService.getFirstFromCollectionType<FAQResponseStrapi.QuestionEtReponse>(RESOURCE_FAQ, query); | ||
|
||
if(isSuccess(strapiQuestionEtReponse)) | ||
return createSuccess(mapQuestionRéponse(strapiQuestionEtReponse.result)); | ||
|
||
return strapiQuestionEtReponse; | ||
} | ||
|
||
async getAllFAQ(): Promise<Either<Array<FAQ.Question>>> { | ||
const query = 'fields[0]=problematique&fields[1]=slug'; | ||
return await this.strapiService.getCollectionType<FAQResponseStrapi.Question, FAQ.Question>(RESOURCE_FAQ, query, mapQuestion); | ||
const strapiQuestions = await this.strapiService.getCollectionType<FAQResponseStrapi.Question>(RESOURCE_FAQ, query); | ||
|
||
if (isSuccess(strapiQuestions)) | ||
return createSuccess(strapiQuestions.result.map((strapiQuestion) => mapQuestion(strapiQuestion))); | ||
|
||
return strapiQuestions; | ||
} | ||
|
||
async listAllFAQSlug(): Promise<Either<Array<FAQ.Slug>>> { | ||
const query = '[fields][0]=slug'; | ||
return await this.strapiService.getCollectionType<FAQResponseStrapi.QuestionSlug, FAQ.Slug>(RESOURCE_FAQ, query, flatMapSlug); | ||
const strapiQuestionSlugs = await this.strapiService.getCollectionType<FAQResponseStrapi.QuestionSlug>(RESOURCE_FAQ, query); | ||
|
||
if (isSuccess(strapiQuestionSlugs)) { | ||
return createSuccess(strapiQuestionSlugs.result.map((strapiQuestionSlug) => strapiQuestionSlug.slug)); | ||
} | ||
|
||
return strapiQuestionSlugs; | ||
} | ||
|
||
} | ||
|