Repository: expo/expo
File: packages/expo-modules-core/ios/Tests/ArrayBufferTests.swift (Swift)

Code before:

  @Suite("JavaScript integration")
  struct JavaScriptIntegrationTests {
    let appContext: AppContext
    var runtime: ExpoRuntime {
      get throws {
        try appContext.runtime
      }
    }

    init() {
      appContext = AppContext.create()
      appContext.moduleRegistry.register(moduleType: ArrayBufferTestModule.self, name: "ArrayBufferTests")
    }

    @Test
    func `creates ArrayBuffer from JavaScript`() throws {
      let jsArrayBuffer = try runtime.eval("new ArrayBuffer(16)").asArrayBuffer()
      let arrayBuffer = try runtime.eval("expo.modules.ArrayBufferTests.createFromJS(new ArrayBuffer(16))").asArrayBuffer()

      #expect(jsArrayBuffer.byteLength == 16)
      #expect(arrayBuffer.byteLength == 16)
    }

    @Test
    func `ArrayBuffer argument accepts Typed Arrays`() throws {
      let result = try runtime.eval([
        "typedArray = new Uint8Array([42, 84])",
        "expo.modules.ArrayBufferTests.readBytesAsArray(typedArray, 2)"
      ]).asArray()

      #expect(try result[0]?.asInt() == 42)
      #expect(try result[1]?.asInt() == 84)
    }

    @Test
    func `returns ArrayBuffer to JavaScript`() throws {
      let buffer = try runtime.eval("expo.modules.ArrayBufferTests.createNative(32)").asArrayBuffer()

      #expect(buffer.byteLength == 32)
    }

    @Test
    func `reads and writes through JavaScript`() throws {
      // Create buffer and write data through JavaScript
      let buffer = try runtime.eval([
        "buffer = expo.modules.ArrayBufferTests.createNative(10)",
        "view = new Uint8Array(buffer)",
        "view[0] = 42",
        "view[1] = 84",
        "buffer"

Review comment (suggestion):
Tests are like second source of docs 😉 
```suggestion
    func `ArrayBuffer argument accepts TypedArray`() throws {
```
or
```suggestion
    func `ArrayBuffer argument accepts typed arrays`() throws {
```

Code after:

  @Suite("JavaScript integration")
  struct JavaScriptIntegrationTests {
    let appContext: AppContext
    var runtime: ExpoRuntime {
      get throws {
        try appContext.runtime
      }
    }

    init() {
      appContext = AppContext.create()
      appContext.moduleRegistry.register(moduleType: ArrayBufferTestModule.self, name: "ArrayBufferTests")
    }

    @Test
    func `creates ArrayBuffer from JavaScript`() throws {
      let jsArrayBuffer = try runtime.eval("new ArrayBuffer(16)").asArrayBuffer()
      let arrayBuffer = try runtime.eval("expo.modules.ArrayBufferTests.createFromJS(new ArrayBuffer(16))").asArrayBuffer()

      #expect(jsArrayBuffer.byteLength == 16)
      #expect(arrayBuffer.byteLength == 16)
    }

    @Test
    func `ArrayBuffer argument accepts full typed arrays`() throws {
      let result = try runtime.eval([
        "typedArray = new Uint8Array([42, 84])",
        "expo.modules.ArrayBufferTests.readBytesAsArray(typedArray, 2)"
      ]).asArray()

      #expect(try result[0]?.asInt() == 42)
      #expect(try result[1]?.asInt() == 84)
    }

    @Test
    func `JavaScriptArrayBuffer accepts partial typed array view`() throws {
      let result = try runtime.eval([
        "arrayBuffer = new Uint8Array([1,2,3,4,5]).buffer",
        "view = new Uint8Array(arrayBuffer, 1, 2)",
        "expo.modules.ArrayBufferTests.readBytesAsArray(view, 2)"
      ]).asArray()

      #expect(try result[0]?.asInt() == 2)
      #expect(try result[1]?.asInt() == 3)
    }

    @Test
    func `NativeArrayBuffer accepts partial typed array view`() throws {
      let result = try runtime.eval([
        "view = new Uint8Array(new Uint8Array([1,2,3,4,5]).buffer, 1, 2)",