def hasInternalComposerCode()

in app/com/gu/contentapi/sanity/ShowBlocksTest.scala [48:83]


  def hasInternalComposerCode(item: Map[String, JsValue]): Boolean =
    item.get("fields").flatMap(fields => (fields \ "internalComposerCode").asOpt[String]).isDefined

  def hasNonEmptyBody(item: Map[String, JsValue]): Boolean =
    item.get("fields").flatMap(fields => (fields \ "body").asOpt[String]).exists(_.trim.nonEmpty)

  def checkBlocks(item: Map[String, JsValue]) = {
    val id = item.getOrElse("id", "ID for item was missing")

    withClue(s"blocks field not found! for item: $id") {
      item should contain key "blocks"
    }

    val blocks = item("blocks")

    // Many articles in CODE don't have a main block
    // (because people don't bother adding a main image when manually testing Composer)
    // so don't check for the presence of a main block.

    val bodyBlocks = (blocks \ "body").asOpt[JsArray]
    withClue(s"blocks.body field not found! for item: $id") {
      bodyBlocks should be (defined)
    }

    // Some content (e.g. cartoons) have an empty body and no body blocks,
    // so only check body blocks if the body exists and is non-empty.
    if (hasNonEmptyBody(item)) {
      withClue(s"blocks.body is an empty list! for item: $id") {
        bodyBlocks.get.value should not be empty
      }
      withClue(s"first body block has no bodyHtml! for item: $id") {
        (bodyBlocks.get.apply(0) \ "bodyHtml").as[String] should not be empty
      }
    }

  }