From 856052e173666f02909e59ede22956d63dff60eb Mon Sep 17 00:00:00 2001
From: Tobias Kohr <tobias.kohr@camptocamp.com>
Date: Thu, 14 Dec 2023 17:20:14 +0100
Subject: [PATCH] test(organisations.component): add unit tests

---
 .../organisations.component.spec.ts           | 75 +++++++++++++++++++
 1 file changed, 75 insertions(+)

diff --git a/libs/feature/catalog/src/lib/organisations/organisations.component.spec.ts b/libs/feature/catalog/src/lib/organisations/organisations.component.spec.ts
index 2e3a5490f4..3b3b7c1635 100644
--- a/libs/feature/catalog/src/lib/organisations/organisations.component.spec.ts
+++ b/libs/feature/catalog/src/lib/organisations/organisations.component.spec.ts
@@ -31,6 +31,15 @@ class OrganisationPreviewMockComponent {
   @Output() clickedOrganisation = new EventEmitter<Organization>()
 }
 
+@Component({
+  selector: 'gn-ui-organisations-result',
+  template: '<div></div>',
+})
+class OrganisationsResultMockComponent {
+  @Input() hits: number
+  @Input() total: number
+}
+
 @Component({
   selector: 'gn-ui-pagination',
   template: '<div></div>',
@@ -43,6 +52,7 @@ class PaginationMockComponent {
 
 class OrganisationsServiceMock {
   organisations$ = of(ORGANISATIONS_FIXTURE)
+  organisationsCount$ = of(ORGANISATIONS_FIXTURE.length)
 }
 
 const organisationMock = {
@@ -66,6 +76,7 @@ describe('OrganisationsComponent', () => {
         OrganisationsFilterMockComponent,
         OrganisationPreviewMockComponent,
         PaginationMockComponent,
+        OrganisationsResultMockComponent,
         ContentGhostComponent,
       ],
       providers: [
@@ -93,6 +104,7 @@ describe('OrganisationsComponent', () => {
 
   describe('on component init', () => {
     let orgPreviewComponents: OrganisationPreviewMockComponent[]
+    let orgResultComponent: OrganisationsResultMockComponent
     let paginationComponentDE: DebugElement
     let setCurrentPageSpy
     let setSortBySpy
@@ -181,6 +193,69 @@ describe('OrganisationsComponent', () => {
         )
       })
     })
+    describe('filter organisations', () => {
+      describe('initial state', () => {
+        beforeEach(() => {
+          orgResultComponent = de.query(
+            By.directive(OrganisationsResultMockComponent).componentInstance
+          )
+        })
+        it('should display number of organisations found to equal all', () => {
+          expect(orgResultComponent.componentInstance.hits).toEqual(
+            ORGANISATIONS_FIXTURE.length
+          )
+        })
+        it('should display number of all organisations', () => {
+          expect(orgResultComponent.componentInstance.total).toEqual(
+            ORGANISATIONS_FIXTURE.length
+          )
+        })
+      })
+      describe('entering search terms', () => {
+        beforeEach(() => {
+          orgResultComponent = de.query(
+            By.directive(OrganisationsResultMockComponent)
+          )
+        })
+        it('should ignore case and display 11 matches for "Data", "DATA" or "data"', () => {
+          component.filterBy$.next('Data')
+          fixture.detectChanges()
+          expect(orgResultComponent.componentInstance.hits).toEqual(11)
+          component.filterBy$.next('DATA')
+          fixture.detectChanges()
+          expect(orgResultComponent.componentInstance.hits).toEqual(11)
+          component.filterBy$.next('data')
+          fixture.detectChanges()
+          expect(orgResultComponent.componentInstance.hits).toEqual(11)
+        })
+        it('should ignore accents and case and display 2 matches for "é Data Org", "e Data Org" or "E Data Org"', () => {
+          component.filterBy$.next('é Data Org')
+          fixture.detectChanges()
+          expect(orgResultComponent.componentInstance.hits).toEqual(2)
+          component.filterBy$.next('e Data Org')
+          fixture.detectChanges()
+          expect(orgResultComponent.componentInstance.hits).toEqual(2)
+          component.filterBy$.next('E Data Org')
+          fixture.detectChanges()
+          expect(orgResultComponent.componentInstance.hits).toEqual(2)
+        })
+        it('should combine multiple termes with "AND" logic and display 1 match for "a data"', () => {
+          component.filterBy$.next('a data')
+          fixture.detectChanges()
+          expect(orgResultComponent.componentInstance.hits).toEqual(1)
+        })
+        it('should combine multiple termes with "AND" logic and display 11 matches for "data org"', () => {
+          component.filterBy$.next('data org')
+          fixture.detectChanges()
+          expect(orgResultComponent.componentInstance.hits).toEqual(11)
+        })
+        it('should display 12 matches for "ORG"', () => {
+          component.filterBy$.next('ORG')
+          fixture.detectChanges()
+          expect(orgResultComponent.componentInstance.hits).toEqual(12)
+        })
+      })
+    })
     describe('click on organisation', () => {
       let orgSelected
       beforeEach(() => {