// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/*
 * This file is automatically loaded and run by Karma because it automatically
 * loads and injects all *.js files it finds.
 */
import type * as Common from '../../../../front_end/core/common/common.js';
import * as ThemeSupport from '../../../../front_end/ui/legacy/theme_support/theme_support.js';
import {resetTestDOM} from '../helpers/DOMHelpers.js';

beforeEach(resetTestDOM);

before(async function() {
  /* Larger than normal timeout because we've seen some slowness on the bots */
  this.timeout(10000);
});

beforeEach(() => {
  // Some unit tests exercise code that assumes a ThemeSupport instance is available.
  // Run this in a beforeEach in case an individual test overrides it.
  const setting = {
    get() {
      return 'default';
    },
  } as Common.Settings.Setting<string>;
  ThemeSupport.ThemeSupport.instance({forceNew: true, setting});
});

// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/*
 * This file is automatically loaded and run by Karma because it automatically
 * loads and injects all *.js files it finds.
 */
import type * as Common from '../../../../front_end/core/common/common.js';
import * as ThemeSupport from '../../../../front_end/ui/legacy/theme_support/theme_support.js';
import {resetTestDOM} from '../helpers/DOMHelpers.js';

beforeEach(resetTestDOM);

before(async function() {
  /* Larger than normal timeout because we've seen some slowness on the bots */
  this.timeout(10000);
});

afterEach(() => {
  // Clear out any Sinon stubs or spies between individual tests.
  sinon.restore();
});

beforeEach(() => {
  // Some unit tests exercise code that assumes a ThemeSupport instance is available.
  // Run this in a beforeEach in case an individual test overrides it.
  const setting = {
    get() {
      return 'default';
    },
  } as Common.Settings.Setting<string>;
  ThemeSupport.ThemeSupport.instance({forceNew: true, setting});
});

Clear sinon stubs between each unit test
Clear sinon stubs between each unit test

Consider the following unit tests:

```
describe.only('jack test', () => {
  const someObj = {
    foo() {
      return 2;
    },
  };
  it('does a thing', () => {
    const stub = sinon.stub(someObj, 'foo').callsFake(() => {
      return 5;
    })
    assert.strictEqual(someObj.foo(), 5); // this test PASSES
  });

  it('does another thing', () => {
    assert.strictEqual(someObj.foo(), 2); // this test FAILS
  });
});
```

The second test fails in our setup because the stub, although it was
created in the first unit test, survives between tests.

To fix this we can call `sinon.restore()`, which ensures that any
stubbed methods get cleaned between runs, and ensures that in the above
test cases both tests pass. `test_setup.ts` is run automatically between
each test, so by adding it to the `afterEach` we ensure each test starts
with a clean slate as far as sinon is concerned.

Fixed: 1275936
Change-Id: Ic218027979b7d5b323d297d90891d741f61d5d88
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3313069
Auto-Submit: Jack Franklin <993fcadce4d04090da2fefd557a0995e7966c8d5@chromium.org>
Commit-Queue: Johan Bay <c4a91c97e1f8a218202ca733f9e9f5122f475e0f@chromium.org>
Reviewed-by: Johan Bay <c4a91c97e1f8a218202ca733f9e9f5122f475e0f@chromium.org>
