Skip to content

Commit

Permalink
Fix test for typeahead
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathonherbert committed Jul 11, 2024
1 parent d0690b0 commit 19d51e7
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 34 deletions.
1 change: 0 additions & 1 deletion prosemirror-client/src/cqlInput/CqlInput.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import userEvent from "@testing-library/user-event";
import {
findByShadowTestId,
findByShadowText,
prettyShadowDOM,
} from "shadow-dom-testing-library";

mock.module("../CqlService", () => ({}));
Expand Down
5 changes: 1 addition & 4 deletions prosemirror-client/src/cqlInput/fixtures/TestCqlService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ const resultFixtures: Record<string, CqlResult> = {
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) {
Expand Down
2 changes: 0 additions & 2 deletions src/main/scala/lang/Cql.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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)
Expand Down
46 changes: 25 additions & 21 deletions src/main/scala/lang/Typeahead.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ case class TypeaheadSuggestion(
suggestions: Suggestions
)


sealed trait Suggestions

case class TextSuggestion(suggestions: List[TextSuggestionOption])
Expand All @@ -32,35 +33,37 @@ 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,
TokenType.QUERY_VALUE -> suggestFieldValue
)

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
)

Expand Down Expand Up @@ -134,6 +137,7 @@ class Typeahead(fieldResolvers: List[TypeaheadField], outputModifierResolvers: L
)
}
case QueryOutputModifier(keyToken, Some(valueToken)) =>

val keySuggestion = suggestOutputModifierKey(
keyToken.literal.getOrElse("")
).map { suggestion =>
Expand Down Expand Up @@ -182,7 +186,7 @@ class Typeahead(fieldResolvers: List[TypeaheadField], outputModifierResolvers: L
TextSuggestion(
outputModifierResolvers
.filter(
_.name.contains(str.toLowerCase())
_.id.contains(str.toLowerCase())
)
.map(_.toSuggestionOption)
)
Expand Down
12 changes: 6 additions & 6 deletions src/test/scala/lang/TypeaheadTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down Expand Up @@ -165,18 +165,18 @@ class TypeaheadTest extends BaseTest {
TextSuggestion(
List(
TextSuggestionOption(
"from-date",
"From date",
"from-date",
"The date to search from"
)
)
)
),
TypeaheadSuggestion(
11,
11,
" ",
DateSuggestion(None, None)
0,
9,
":",
TextSuggestion(List.empty)
)
)
}
Expand Down

0 comments on commit 19d51e7

Please sign in to comment.