From 19d51e7bc4df7f8e723e988d9283b957c931d2cf Mon Sep 17 00:00:00 2001 From: Jonathon Herbert Date: Thu, 11 Jul 2024 10:12:55 +0100 Subject: [PATCH] Fix test for typeahead --- .../src/cqlInput/CqlInput.spec.ts | 1 - .../src/cqlInput/fixtures/TestCqlService.ts | 5 +- src/main/scala/lang/Cql.scala | 2 - src/main/scala/lang/Typeahead.scala | 46 ++++++++++--------- src/test/scala/lang/TypeaheadTest.scala | 12 ++--- 5 files changed, 32 insertions(+), 34 deletions(-) diff --git a/prosemirror-client/src/cqlInput/CqlInput.spec.ts b/prosemirror-client/src/cqlInput/CqlInput.spec.ts index fb48a7f..52bdb17 100644 --- a/prosemirror-client/src/cqlInput/CqlInput.spec.ts +++ b/prosemirror-client/src/cqlInput/CqlInput.spec.ts @@ -10,7 +10,6 @@ import userEvent from "@testing-library/user-event"; import { findByShadowTestId, findByShadowText, - prettyShadowDOM, } from "shadow-dom-testing-library"; mock.module("../CqlService", () => ({})); diff --git a/prosemirror-client/src/cqlInput/fixtures/TestCqlService.ts b/prosemirror-client/src/cqlInput/fixtures/TestCqlService.ts index ff64bd9..fd54662 100644 --- a/prosemirror-client/src/cqlInput/fixtures/TestCqlService.ts +++ b/prosemirror-client/src/cqlInput/fixtures/TestCqlService.ts @@ -28,10 +28,7 @@ const resultFixtures: Record = { export class TestCqlService implements CqlServiceInterface { private abortController: AbortController | undefined; - /** - * @param url - * @param resultFixtures A map from query strings to results - */ + // @ts-ignore unused var – url not relevant for test constructor(private url: string) {} public setUrl(url: string) { diff --git a/src/main/scala/lang/Cql.scala b/src/main/scala/lang/Cql.scala index f07a114..4526b68 100644 --- a/src/main/scala/lang/Cql.scala +++ b/src/main/scala/lang/Cql.scala @@ -4,7 +4,6 @@ import scala.util.{Failure, Success, Try} import io.circe.generic.semiauto.* import scala.concurrent.Future -import com.gu.contentapi.client.GuardianContentClient case class CqlResult( tokens: List[Token], @@ -19,7 +18,6 @@ case class CqlResult( class Cql(typeahead: Typeahead): implicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global - val guardianContentClient = new GuardianContentClient("test") def run(program: String): Future[CqlResult] = val scanner = new Scanner(program) diff --git a/src/main/scala/lang/Typeahead.scala b/src/main/scala/lang/Typeahead.scala index d4222fd..35f69c2 100644 --- a/src/main/scala/lang/Typeahead.scala +++ b/src/main/scala/lang/Typeahead.scala @@ -13,6 +13,7 @@ case class TypeaheadSuggestion( suggestions: Suggestions ) + sealed trait Suggestions case class TextSuggestion(suggestions: List[TextSuggestionOption]) @@ -32,25 +33,28 @@ type TypeaheadResolver = (String => Future[List[TextSuggestionOption]]) | type TypeaheadType = "TEXT" | "DATE" - case class TypeaheadField( - id: String, - name: String, - description: String, - resolver: TypeaheadResolver, - suggestionType: "TEXT" | "DATE" = "TEXT" - ): - def resolveSuggestions(str: String): Future[List[TextSuggestionOption]] = - resolver match { - case list: List[TextSuggestionOption] => - Future.successful(list.filter { _.label.contains(str) }) - case resolverFn: (String => Future[List[TextSuggestionOption]]) => - resolverFn(str) - } +case class TypeaheadField( + id: String, + name: String, + description: String, + resolver: TypeaheadResolver, + suggestionType: "TEXT" | "DATE" = "TEXT" +): + def resolveSuggestions(str: String): Future[List[TextSuggestionOption]] = + resolver match { + case list: List[TextSuggestionOption] => + Future.successful(list.filter { _.label.contains(str) }) + case resolverFn: (String => Future[List[TextSuggestionOption]]) => + resolverFn(str) + } - def toSuggestionOption: TextSuggestionOption = - TextSuggestionOption(name, name, description) + def toSuggestionOption: TextSuggestionOption = + TextSuggestionOption(name, id, description) -class Typeahead(fieldResolvers: List[TypeaheadField], outputModifierResolvers: List[TypeaheadField]) { +class Typeahead( + fieldResolvers: List[TypeaheadField], + outputModifierResolvers: List[TypeaheadField] +) { private val typeaheadTokenResolverMap = Map( TokenType.QUERY_FIELD_KEY -> suggestFieldKey, TokenType.QUERY_OUTPUT_MODIFIER_KEY -> suggestOutputModifierKey, @@ -58,9 +62,8 @@ class Typeahead(fieldResolvers: List[TypeaheadField], outputModifierResolvers: L ) private val typeaheadFieldEntries = TextSuggestion( - fieldResolvers.map { - case TypeaheadField(id, label, description, _, _) => - TextSuggestionOption(label, id, description) + fieldResolvers.map { case TypeaheadField(id, label, description, _, _) => + TextSuggestionOption(label, id, description) }.toList ) @@ -134,6 +137,7 @@ class Typeahead(fieldResolvers: List[TypeaheadField], outputModifierResolvers: L ) } case QueryOutputModifier(keyToken, Some(valueToken)) => + val keySuggestion = suggestOutputModifierKey( keyToken.literal.getOrElse("") ).map { suggestion => @@ -182,7 +186,7 @@ class Typeahead(fieldResolvers: List[TypeaheadField], outputModifierResolvers: L TextSuggestion( outputModifierResolvers .filter( - _.name.contains(str.toLowerCase()) + _.id.contains(str.toLowerCase()) ) .map(_.toSuggestionOption) ) diff --git a/src/test/scala/lang/TypeaheadTest.scala b/src/test/scala/lang/TypeaheadTest.scala index 05c55a1..cb82f79 100644 --- a/src/test/scala/lang/TypeaheadTest.scala +++ b/src/test/scala/lang/TypeaheadTest.scala @@ -7,7 +7,7 @@ class TypeaheadTest extends BaseTest { describe("typeahead") { val typeaheadQueryClient = new TestTypeaheadHelpers() val typeahead = - new Typeahead(typeaheadQueryClient.fieldResolvers, List.empty) + new Typeahead(typeaheadQueryClient.fieldResolvers, typeaheadQueryClient.outputModifierResolvers) it("should give no typeahead where none is warranted") { typeahead.getSuggestions(QueryList(List.empty)).map { result => @@ -165,7 +165,7 @@ class TypeaheadTest extends BaseTest { TextSuggestion( List( TextSuggestionOption( - "from-date", + "From date", "from-date", "The date to search from" ) @@ -173,10 +173,10 @@ class TypeaheadTest extends BaseTest { ) ), TypeaheadSuggestion( - 11, - 11, - " ", - DateSuggestion(None, None) + 0, + 9, + ":", + TextSuggestion(List.empty) ) ) }