Skip to content

Commit

Permalink
Make xcode tests actually respect the result
Browse files Browse the repository at this point in the history
  • Loading branch information
nakajima committed May 3, 2023
1 parent c7b0501 commit cae81d2
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; };
3E461D99554A48A4959DE609 /* SplashScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AA286B85B6C04FC6940260E9 /* SplashScreen.storyboard */; };
A6A5DB882A00551E001DF8C2 /* xmtpreactnativesdkexampleUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6A5DB872A00551E001DF8C2 /* xmtpreactnativesdkexampleUITests.swift */; };
A6A5DB8A2A00551E001DF8C2 /* xmtpreactnativesdkexampleUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6A5DB892A00551E001DF8C2 /* xmtpreactnativesdkexampleUITestsLaunchTests.swift */; };
B18059E884C0ABDD17F3DC3D /* ExpoModulesProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAC715A2D49A985799AEE119 /* ExpoModulesProvider.swift */; };
BB2F792D24A3F905000567C9 /* Expo.plist in Resources */ = {isa = PBXBuildFile; fileRef = BB2F792C24A3F905000567C9 /* Expo.plist */; };
C90474A97E544153BED95C2A /* noop-file.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDF0078FD601458DA88B0565 /* noop-file.swift */; };
Expand Down Expand Up @@ -42,7 +41,6 @@
80323D3D45487AD85F64F249 /* Pods-xmtpreactnativesdkexample.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-xmtpreactnativesdkexample.debug.xcconfig"; path = "Target Support Files/Pods-xmtpreactnativesdkexample/Pods-xmtpreactnativesdkexample.debug.xcconfig"; sourceTree = "<group>"; };
A6A5DB852A00551E001DF8C2 /* xmtpreactnativesdkexampleUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = xmtpreactnativesdkexampleUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
A6A5DB872A00551E001DF8C2 /* xmtpreactnativesdkexampleUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = xmtpreactnativesdkexampleUITests.swift; sourceTree = "<group>"; };
A6A5DB892A00551E001DF8C2 /* xmtpreactnativesdkexampleUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = xmtpreactnativesdkexampleUITestsLaunchTests.swift; sourceTree = "<group>"; };
AA286B85B6C04FC6940260E9 /* SplashScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = SplashScreen.storyboard; path = xmtpreactnativesdkexample/SplashScreen.storyboard; sourceTree = "<group>"; };
BB2F792C24A3F905000567C9 /* Expo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Expo.plist; sourceTree = "<group>"; };
ED297162215061F000B7C4FE /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = System/Library/Frameworks/JavaScriptCore.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -138,7 +136,6 @@
isa = PBXGroup;
children = (
A6A5DB872A00551E001DF8C2 /* xmtpreactnativesdkexampleUITests.swift */,
A6A5DB892A00551E001DF8C2 /* xmtpreactnativesdkexampleUITestsLaunchTests.swift */,
);
path = xmtpreactnativesdkexampleUITests;
sourceTree = "<group>";
Expand Down Expand Up @@ -384,7 +381,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
A6A5DB8A2A00551E001DF8C2 /* xmtpreactnativesdkexampleUITestsLaunchTests.swift in Sources */,
A6A5DB882A00551E001DF8C2 /* xmtpreactnativesdkexampleUITests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,34 @@ final class xmtpreactnativesdkexampleUITests: XCTestCase {
// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}

func testExample() throws {
func testRunTests() throws {
// UI tests must launch the application that they test.
let app = XCUIApplication()
app.launch()

let button = app.buttons["Enable Test Mode"]
// Go to unit tests page
let button = app.buttons["Unit tests"]
XCTAssert(button.waitForExistence(timeout: 3))
button.tap()

// Make sure we're there
let view = app.staticTexts["Test View"]
XCTAssert(view.waitForExistence(timeout: 3))

let result = app.staticTexts["result"]
XCTAssert(result.waitForExistence(timeout: 3))
// Wait for tests to complete
let complete = app.staticTexts["tests-complete"]
XCTAssert(complete.waitForExistence(timeout: 5))

let input = app.textFields["input"]
XCTAssert(input.waitForExistence(timeout: 3))

// See if we have any failures
if app.staticTexts["FAIL"].waitForExistence(timeout: 3) {
// Take a screenshot so we can see what failed in the UI
let screenshot = app.windows.firstMatch.screenshot()
let attachment = XCTAttachment(screenshot: screenshot)
attachment.lifetime = .keepAlways
add(attachment)

XCTFail("Tests failed.")
}
}

}

This file was deleted.

52 changes: 46 additions & 6 deletions example/src/TestsView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import { tests, Test } from "./tests";

type Result = "waiting" | "running" | "success" | "failure" | "error";

function TestView({ test }: { test: Test }): JSX.Element {
function TestView({
test,
onComplete,
}: {
test: Test;
onComplete: () => void;
}): JSX.Element {
const [markedComplete, setMarkedComplete] = useState<boolean>(false);
const [result, setResult] = useState<Result>("waiting");
const [errorMessage, setErrorMessage] = useState<string>("");

Expand All @@ -24,6 +31,11 @@ function TestView({ test }: { test: Test }): JSX.Element {
setErrorMessage(JSON.stringify(err));
}
}

if (!markedComplete) {
onComplete();
setMarkedComplete(true);
}
}

useEffect(() => {
Expand Down Expand Up @@ -56,28 +68,56 @@ function TestView({ test }: { test: Test }): JSX.Element {
title={result == "running" ? "Running..." : "Run"}
/>
</View>
{result == "failure" && (
<Text
testID="FAIL"
style={{ paddingHorizontal: 12, paddingBottom: 12 }}
>
{test.name} failed
</Text>
)}
{errorMessage && (
<View style={{ paddingHorizontal: 12, paddingBottom: 12 }}>
<Text style={{ color: "#721c24" }}>Error: {errorMessage}</Text>
<Text testID="FAIL" style={{ color: "#721c24" }}>
Error: {errorMessage}
</Text>
</View>
)}
</View>
);
}

export default function TestsView(): JSX.Element {
const [text, setText] = useState<string>("");
const [completedTests, setCompletedTests] = useState<number>(0);

return (
<ScrollView>
<View>
<View style={{ padding: 12 }}>
<Text>Test View</Text>
<Text testID="Test View">This view is used by unit tests.</Text>
<Text testID="Test View">Unit Tests</Text>
<View
style={{ flexDirection: "row", justifyContent: "space-between" }}
>
<Text>
Running {completedTests}/{tests.length}
</Text>

{completedTests == tests.length && (
<Text testID="tests-complete">Done</Text>
)}
</View>
</View>
<View testID="tests" style={{ paddingHorizontal: 12 }}>
{tests.map((test: Test, i) => {
return <TestView test={test} key={i} />;
return (
<TestView
test={test}
onComplete={() => {
setCompletedTests((prev) => prev + 1);
}}
key={i}
/>
);
})}
</View>
</View>
Expand Down
4 changes: 4 additions & 0 deletions example/src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ function test(name: string, perform: () => Promise<boolean>) {
tests.push({ name, run: perform });
}

// test("can fail", async () => {
// return false;
// });

test("can make a client", async () => {
const client = await XMTP.Client.createRandom("local");
return client.address.length > 0;
Expand Down

0 comments on commit cae81d2

Please sign in to comment.