46 lines
1.6 KiB
C++
46 lines
1.6 KiB
C++
#include <gtest/gtest.h>
|
|
#include <QSignalSpy>
|
|
#include <QVariant>
|
|
#include "api/NavRequest.hpp"
|
|
#include "api/NavLoads.hpp"
|
|
#include "net/FakeApiCall.hpp"
|
|
|
|
using namespace geopro::data;
|
|
using geopro::net::ApiResponse;
|
|
using geopro::net::test::FakeApiCall;
|
|
|
|
namespace {
|
|
ApiResponse ok() { ApiResponse r; r.code = 200; r.httpStatus = 200; return r; }
|
|
ApiResponse bad() { ApiResponse r; r.code = 500; r.httpStatus = 200; r.msg = QStringLiteral("boom"); return r; }
|
|
auto isFailure = [](const ApiResponse& r) { return r.code != 200 || !r.rawError.isEmpty(); };
|
|
} // namespace
|
|
|
|
TEST(NavRequest, EmitsDoneWithParsedPayload) {
|
|
auto* call = new FakeApiCall;
|
|
auto* req = new ApiNavRequest(call,
|
|
[](const ApiResponse&) { return QVariant::fromValue(DsPage{{}, 42}); }, isFailure);
|
|
QSignalSpy doneSpy(req, &NavRequest::done);
|
|
call->fire(ok());
|
|
ASSERT_EQ(doneSpy.count(), 1);
|
|
const auto page = qvariant_cast<DsPage>(doneSpy.takeFirst().at(0));
|
|
EXPECT_EQ(page.total, 42);
|
|
}
|
|
|
|
TEST(NavRequest, EmitsFailedOnBusinessError) {
|
|
auto* call = new FakeApiCall;
|
|
auto* req = new ApiNavRequest(call, [](const ApiResponse&) { return QVariant(); }, isFailure);
|
|
QSignalSpy failSpy(req, &NavRequest::failed);
|
|
call->fire(bad());
|
|
EXPECT_EQ(failSpy.count(), 1);
|
|
}
|
|
|
|
TEST(NavRequest, AbortSuppressesLateDone) {
|
|
auto* call = new FakeApiCall;
|
|
auto* req = new ApiNavRequest(call, [](const ApiResponse&) { return QVariant(); }, isFailure);
|
|
QSignalSpy doneSpy(req, &NavRequest::done);
|
|
req->abort();
|
|
EXPECT_TRUE(call->aborted);
|
|
call->fire(ok());
|
|
EXPECT_EQ(doneSpy.count(), 0);
|
|
}
|