Skip to content

Commit

Permalink
Merge pull request #3113 from wordpress-mobile/issue/11236-customer-l…
Browse files Browse the repository at this point in the history
…ast-name

Order form is dropping second last names unexpectedly
  • Loading branch information
kidinov authored Nov 27, 2024
2 parents 3b731c3 + 2aca001 commit d01056e
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,184 @@ class WCCustomerMapperTest {
// THEN
assertThat(result.isPayingCustomer).isEqualTo(false)
}

@Test
fun `given customer name, then first name is properly assigned`() {
// GIVEN
val siteId = 23
val site = SiteModel().apply { id = siteId }

val customerDTO = CustomerFromAnalyticsDTO(name = "firstname lastname")

// WHEN
val result = mapper.mapToModel(site, customerDTO)

// THEN
assertEquals("firstname", result.firstName)
}

@Test
fun `given customer name as null, then first name returns empty string`() {
// GIVEN
val siteId = 23
val site = SiteModel().apply { id = siteId }

val customerDTO = CustomerFromAnalyticsDTO(name = null)

// WHEN
val result = mapper.mapToModel(site, customerDTO)

// THEN
assertEquals("", result.firstName)
}

@Test
fun `given customer name with no space, then first name returns full string`() {
// GIVEN
val siteId = 23
val site = SiteModel().apply { id = siteId }

val customerDTO = CustomerFromAnalyticsDTO(name = "firstnamelastname")

// WHEN
val result = mapper.mapToModel(site, customerDTO)

// THEN
assertEquals("firstnamelastname", result.firstName)
}

@Test
fun `given customer name, then last name is properly assigned`() {
// GIVEN
val siteId = 23
val site = SiteModel().apply { id = siteId }

val customerDTO = CustomerFromAnalyticsDTO(name = "firstname lastname")

// WHEN
val result = mapper.mapToModel(site, customerDTO)

// THEN
assertEquals("lastname", result.lastName)
}

@Test
fun `given customer name as null, then last name returns empty string`() {
// GIVEN
val siteId = 23
val site = SiteModel().apply { id = siteId }

val customerDTO = CustomerFromAnalyticsDTO(name = null)

// WHEN
val result = mapper.mapToModel(site, customerDTO)

// THEN
assertEquals("", result.lastName)
}

@Test
fun `given customer name has no space, then last name returns empty string`() {
// GIVEN
val siteId = 23
val site = SiteModel().apply { id = siteId }

val customerDTO = CustomerFromAnalyticsDTO(name = "firstnamelastname")

// WHEN
val result = mapper.mapToModel(site, customerDTO)

// THEN
assertEquals("", result.lastName)
}

@Test
fun `given customer name has multiple spaces, then first name returns proper string`() {
// GIVEN
val siteId = 23
val site = SiteModel().apply { id = siteId }

val customerDTO = CustomerFromAnalyticsDTO(name = "firstname and a very long last name")

// WHEN
val result = mapper.mapToModel(site, customerDTO)

// THEN
assertEquals("firstname", result.firstName)
}

@Test
fun `given customer name has multiple spaces, then last name returns proper string`() {
// GIVEN
val siteId = 23
val site = SiteModel().apply { id = siteId }

val customerDTO = CustomerFromAnalyticsDTO(name = "firstname and a very long last name")

// WHEN
val result = mapper.mapToModel(site, customerDTO)

// THEN
assertEquals("and a very long last name", result.lastName)
}

@Test
fun `given customer name has leading space, then first name returns proper string`() {
// GIVEN
val siteId = 23
val site = SiteModel().apply { id = siteId }

val customerDTO = CustomerFromAnalyticsDTO(name = " firstname and a very long last name")

// WHEN
val result = mapper.mapToModel(site, customerDTO)

// THEN
assertEquals("firstname", result.firstName)
}

@Test
fun `given customer name has trailing space, then last name returns proper string`() {
// GIVEN
val siteId = 23
val site = SiteModel().apply { id = siteId }

val customerDTO = CustomerFromAnalyticsDTO(name = "firstname and a very long last name ")

// WHEN
val result = mapper.mapToModel(site, customerDTO)

// THEN
assertEquals("and a very long last name", result.lastName)
}

@Test
fun `given customer name has multiple in between space, then first name returns proper string`() {
// GIVEN
val siteId = 23
val site = SiteModel().apply { id = siteId }

val customerDTO = CustomerFromAnalyticsDTO(name = " firstname and a very long last name")

// WHEN
val result = mapper.mapToModel(site, customerDTO)

// THEN
assertEquals("firstname", result.firstName)
}

@Test
fun `given customer name has multiple in between space, then last name returns proper string`() {
// GIVEN
val siteId = 23
val site = SiteModel().apply { id = siteId }

val customerDTO = CustomerFromAnalyticsDTO(name = " firstname and a very long last name")

// WHEN
val result = mapper.mapToModel(site, customerDTO)

// THEN
assertEquals("and a very long last name", result.lastName)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ class WCCustomerMapper @Inject constructor() {
}
}

private fun String?.firstNameFromName() = this?.split(" ")?.firstOrNull() ?: ""
private fun String?.lastNameFromName() = this?.split(" ")?.lastOrNull() ?: ""
// Please refer WCCustomerMapperTest file which serves as documentation of how this function behaves.
private fun String?.firstNameFromName(): String =
this?.trim()?.substringBefore(' ')?.trim().orEmpty()

private fun String?.lastNameFromName(): String =
this?.trim()?.substringAfter(' ', "")?.trim().orEmpty()
}

0 comments on commit d01056e

Please sign in to comment.